Keeping selected row after sort
-
DataGrid, DataView, Sort - All great things. What I'm having trouble doing is keep my selected row selected after I column sort. The selectedIndex is no longer valid because of the sort. So how do I find the current row index of the previously selected item. ?
-
DataGrid, DataView, Sort - All great things. What I'm having trouble doing is keep my selected row selected after I column sort. The selectedIndex is no longer valid because of the sort. So how do I find the current row index of the previously selected item. ?
i am assuming that you are sorting through the header links on top of each coloumn. if this is the case, a postback will be performed every time you sort. Hence your page will render again and any your changes will revert unless they are static or persistent. now am i no expert but the solution for your problem in my mind is the use of the viewstate. just store the selectedindex in a viewstate and extract its value when you bind your datagrid in the pageload event. hope this helps! best of luck and regards! ☺«««DTA»»»☺
-
DataGrid, DataView, Sort - All great things. What I'm having trouble doing is keep my selected row selected after I column sort. The selectedIndex is no longer valid because of the sort. So how do I find the current row index of the previously selected item. ?
Hi there, The simple way is to use the
DataView.Find
method to query the index of the previously selected row. Basically, this method searches for a particular row in the DataView by the specified sort key value, so it would always give you the correct index of the row which you expect if the sort key value is unique. If the column specified in the Sort property do not contain unique values, then you may think of another way. For example, you can walk through all the items(DataRowView), then for each item you compare it with the previously selected row to figure out the index after you do the sorting on the dataview. In addition, you can walk through the Items collection of the datagrid after you bind the sorted datasource to the control to find the new index. Once you have the new index value, you can set the SelectedIndex property to that value. Just some ideas. -
Hi there, The simple way is to use the
DataView.Find
method to query the index of the previously selected row. Basically, this method searches for a particular row in the DataView by the specified sort key value, so it would always give you the correct index of the row which you expect if the sort key value is unique. If the column specified in the Sort property do not contain unique values, then you may think of another way. For example, you can walk through all the items(DataRowView), then for each item you compare it with the previously selected row to figure out the index after you do the sorting on the dataview. In addition, you can walk through the Items collection of the datagrid after you bind the sorted datasource to the control to find the new index. Once you have the new index value, you can set the SelectedIndex property to that value. Just some ideas...thank you for your thoughts. I can't use
DataView.Find
as the sort key doesn't return a unique values. The table has a unique identifier. ID. This ID is a column in theDataGrid
at present but won't be in the finished version. I'm use showing it to help debug. The user selects a row, triggers an event, I saved the ID of the selected row and highlight it in theDataGrid
Then the user clicks a column to sort theDataGrid
and the follow code is performed.DataView dv = dataSet.MyTable.DefaultView; dv.Sort = "column"; DataGrid1.DataSource = dv; DataGrid1.Bind();
How can I then loop through and find (using the ID) which row in the DataGrid or DataView contains the row of the selected item. Which I will use to setDataGrid1.SelectedIndex
. It isn't clear to me that this is possible. -
..thank you for your thoughts. I can't use
DataView.Find
as the sort key doesn't return a unique values. The table has a unique identifier. ID. This ID is a column in theDataGrid
at present but won't be in the finished version. I'm use showing it to help debug. The user selects a row, triggers an event, I saved the ID of the selected row and highlight it in theDataGrid
Then the user clicks a column to sort theDataGrid
and the follow code is performed.DataView dv = dataSet.MyTable.DefaultView; dv.Sort = "column"; DataGrid1.DataSource = dv; DataGrid1.Bind();
How can I then loop through and find (using the ID) which row in the DataGrid or DataView contains the row of the selected item. Which I will use to setDataGrid1.SelectedIndex
. It isn't clear to me that this is possible.Assume that the ID column is of the Integer type, the sample code goes like this to query the index of the previously selected row in the sorted dataview:
int id = ...;//<-- the id of the previously selected row.
int index = -1;
IEnumerator enu = dv.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; -
Assume that the ID column is of the Integer type, the sample code goes like this to query the index of the previously selected row in the sorted dataview:
int id = ...;//<-- the id of the previously selected row.
int index = -1;
IEnumerator enu = dv.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;I've learnt some great things here, thanks. I'm interested in why the DataView.Enumerator returns the results in the correct sorted order where as the DataView.Table.Rows list is always in the unsorted order. Best regards