access rows in order that they appear after sorting datagrid
-
Hello, I am trying to print out a datagrid, and I have code that works fine, but it won't print the data in sorted order if it has been sorted in the datagrid. It still prints the original order. How can I access the rows in sorted order instead of in the original order? theTable.Rows[i] is what I've been using to loop through the rows, but is there anything else I can use so that I can print the grid sorted. Thanks, Blake
-
Hello, I am trying to print out a datagrid, and I have code that works fine, but it won't print the data in sorted order if it has been sorted in the datagrid. It still prints the original order. How can I access the rows in sorted order instead of in the original order? theTable.Rows[i] is what I've been using to loop through the rows, but is there anything else I can use so that I can print the grid sorted. Thanks, Blake
DataTable.Rows
will always present the original order, but you can use aDataView
to sort and get the sorted order of the containedDataRow
s. You can do this like so:DataView view = new DataView(myDataTable);
view.Sort = "myIDColumn";
foreach (DataRowView rowView in view)
Console.WriteLine(rowView.Row.ItemArray);You should be able to get this using
theTable.DefaultView
, which should return theDataView
in its current state that theDataGrid
used to sort or filter the data.Microsoft MVP, Visual C# My Articles
-
DataTable.Rows
will always present the original order, but you can use aDataView
to sort and get the sorted order of the containedDataRow
s. You can do this like so:DataView view = new DataView(myDataTable);
view.Sort = "myIDColumn";
foreach (DataRowView rowView in view)
Console.WriteLine(rowView.Row.ItemArray);You should be able to get this using
theTable.DefaultView
, which should return theDataView
in its current state that theDataGrid
used to sort or filter the data.Microsoft MVP, Visual C# My Articles