Reputation: 11
I am implementing a multi-column sort on a datagridview bound to a datatable. The datagridview uses a combobox for one of the columns where the display member is a string from another table and the Value member is an integer which provides the foreign key. In a separate datagridview, I can sort easily using a RowComparer via ICompare and the FormattedValue property. However, the main datagridview is bound and won't let me use that interface. Therefore, I am trying to sort the underlying data something like below using a sort string as follows, but that can sort the lookup field, not the ID field. Any ideas?
On the form:
private void customSortToolStripMenuItem_Click(object sender, EventArgs e)
{
string sortString = "SectionID ASC, Shift DESC"; //I want the name not ID
if (sortString != "")
{
DataView view = localDataSet.myBaseTable.DefaultView;
view.Sort = sortString;
tableBindingSource.DataSource = view;
}
}
Upvotes: 1
Views: 722
Reputation: 23831
Perhaps LINQ will give you better access here
DataTable dt = new DataTable();
var result = dt.AsEnumerable()
.OrderBy(row => row.Field<decimal>("ColumnNameA"))
.OrderByDescending(row => row.Field<decimal>("ColumnNameB"));
I hope this helps.
Upvotes: 0