Question concerning Sorting DataGrids
-
Hello, In Windows Forms, I have a datagrid that is sortable by clicking on the column headers. Say 2nd record is already selected and I sort the datagrid. Now this previously selected row is unselected. At this point how do I get the index to this previously selected row to have it selected again. The issue I'm referring is that after sorting the dataset still has the same indexing order on the datagrid, meaning, a particular row's index before or after sorting remains intact eventhough after sorting it's position on the datagrid view has changed. Thank you.
-
Hello, In Windows Forms, I have a datagrid that is sortable by clicking on the column headers. Say 2nd record is already selected and I sort the datagrid. Now this previously selected row is unselected. At this point how do I get the index to this previously selected row to have it selected again. The issue I'm referring is that after sorting the dataset still has the same indexing order on the datagrid, meaning, a particular row's index before or after sorting remains intact eventhough after sorting it's position on the datagrid view has changed. Thank you.
I'm trying to do the same thing. If you find out a way do it let me know, Thank you
-
Hello, In Windows Forms, I have a datagrid that is sortable by clicking on the column headers. Say 2nd record is already selected and I sort the datagrid. Now this previously selected row is unselected. At this point how do I get the index to this previously selected row to have it selected again. The issue I'm referring is that after sorting the dataset still has the same indexing order on the datagrid, meaning, a particular row's index before or after sorting remains intact eventhough after sorting it's position on the datagrid view has changed. Thank you.
Two part to this. Part1 (Save Row ID) Each time a user selects a row in your DataGrid you need to save the row's unique idenifier. Part2 (Re select Row based on ID) After sorting, locate the new location of your selected row using the saved unique row identifier. Example Part 1
// set selected row's id in view state
IEnumerator enu = ((DataView)DataGrid1.DataSource).GetEnumerator();
int index = 0;
while( index <= DataGrid1.SelectedIndex )
{
enu.MoveNext();
index++;
}DataRowView rowView = enu.Current as DataRowView;
DataRow row = rowView.Row;
ViewState["item"] = (int)row["ID"];Example Part 2
DataView sortView = dataSet1.MyTable.DefaultView;
// numberDiv is a static int
if( ( numberDiv % 2 ) == 0 )
{
sortView.Sort = e.SortExpression + " ASC";
}
else
{
sortView.Sort = e.SortExpression + " DESC";
}
numberDiv++;// set new sort into ViewState
ViewState["sort"] = sortView.Sort;// set sorted dataview
DataGrid1.DataSource = sortView;// bind data
DataGrid1.DataBind();// here reselected your row
// check that you have an item selected
if( ViewState["item"] != null )
{
int id = (int)ViewState["item"];
int index = -1;
IEnumerator enu = sortView.GetEnumerator();
while(enu.MoveNext())
{
index++;
DataRowView rowView = enu.Current as DataRowView;
DataRow row = rowView.Row;
int rowID = (int)row["ID"];
if(rowID == id)
{
break;
}
}
DataGrid1.SelectedIndex = index;
}Hope this helps. My thanks to minhpc_bk