Irakli Lekishvili
Irakli Lekishvili

Reputation: 34158

Unable to sort datagridview

Hello i cannot sort alphabetical my datagridview

this is how i fill my grid :

 bs = new BindingSource();
                bs.DataSource = db.GetProducts.ToList();
                dgvInventory.DataSource = bs;

and this is how i try to sort my grid :

     private void toolStripButton3_Click_1(object sender, EventArgs e)
    {
        bs.Sort = "ID DESC, Name ASC";
        dgvInventory.DataSource = bs;
    }

And when i pressing button nothing happens This two columns is exsist in data grid

and this is screen :

enter image description here

Upvotes: 2

Views: 6648

Answers (2)

CodingGorilla
CodingGorilla

Reputation: 19842

Quoting from: http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.sort.aspx

To support sorting, the underlying list must implement the IBindingList or IBindingListView interfaces. This capability can be queried through the SupportsSorting property. Multicolumn sorting is available when the SupportsAdvancedSorting property is true.

You're calling the ToList() extension method which is going to return you a List<Product> which is not going to support either of those interfaces and hence won't be sortable.

Upvotes: 6

Filip Popović
Filip Popović

Reputation: 2655

When You have custom objects, You have to implement SortableBindingList. Do a search on the Internet for this. The reason for such behaviour is that underlying source is responsible for sorting, not DataGridView.

Also, the same question here: DataGridView Sort does not work

Upvotes: 1

Related Questions