If I write:
myDataGridView.DataSource = myDataTable.Select(fitlerString);
myDataGridView.Sort(myDataGridView.Columns[1], ListSortDirection.Ascending)
I get an InvalidOpera开发者_如何学编程tionException
with the message
"DataGridView control must be bound to an IBindingList object to be sorted."
but I can't find a way to get a Datatable (instead of a DataRow[]
) with only the desired rows out of the DataTable.Select()
call.
Can I use DataTable.Select(filterString)
to control the displayed rows in a DataGridView and also use DataGridView.Sort()
to control the order they're displayed in?
If it's not going to cause problems elsewhere I'd prefer to be able to keep my DataSource as a DataTable to avoid having to change assumptions elsewhere in the codebase.
You are going to have to suffle around your data. Good news is I found this blog entry by Wessam Zeidan which has the code already written. The problem he is solving is just a bit different but I think it will help you. The main take-away is converting the DataRow[] object back into a DataTable. Be sure to read the comments as well as they may help.
Good luck and Good sorting!
精彩评论