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开发者_StackOverflow nothing happens This two columns is exsist in data grid
and this is screen :
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.
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
精彩评论