We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

How to keep the highlight of a record after sorting (by double-click on every header)?

Hi, I have a table with 10 columns. SSN (Social Security Number) is one of them. Before sorting, the record which has SSN=000-00-0020, is being highlighted. However, after sorting, highligh is at the first record instead of SSN=000-00-0020. How does SSN=00-00-0020 still keep the highlight? Thank a lot!

7 Replies

AD Administrator Syncfusion Team February 22, 2005 09:57 AM UTC

You will have to handle this yourself, saving the selected row before the sort, and resetting it after the sort somehow. Here is a forum thread that discusses this. http://www.syncfusion.com/Support/Forums/message.aspx?MessageID=10655


AD Administrator Syncfusion Team February 22, 2005 06:50 PM UTC

Clay, What would be the way when the PK has more than one column? Regards, Thomas >You will have to handle this yourself, saving the selected row before the sort, and resetting it after the sort somehow. Here is a forum thread that discusses this. > >http://www.syncfusion.com/Support/Forums/message.aspx?MessageID=10655


AD Administrator Syncfusion Team February 22, 2005 07:20 PM UTC

There may be a better way, but here is one way that I think would work. Get the DataView from the grid''s CurrencyManager, and directly loop through the DataView looking for the row with the right primary keys. Looping through the DataView will be much faster than looping through the grid itself as it avoids the grid events required to retrieve the data. Once you have the position in the CUrrencyManager, you can use grid.Binder.PositionToRowIndex to get the proper row. To get the DataView, you can use CurrencyManager cm = (CurrencyManager)this.grid.BindingContext[grid.DataSource, grid.DataMember]; DataView dv = (DataView) cm.List; //loop through dv looking for PK values //


DB David Bosak February 23, 2005 07:20 AM UTC

Thanks a lot, it worked... I also corrected some lines for my codes: copyGrid.SortBehavior = GridSortBehavior.None; then call the method: private void KeepHighLightAfterSorting(MyGridDataBoundGrid grid, object sender, GridCellClickEventArgs e, int lastRowBeforeSorting) { int row, col; grid.PointToRowCol(new Point(e.MouseEventArgs.X, e.MouseEventArgs.Y), out row, out col, -1); if(row == 0 && col > 0) { //Save the current position CurrencyManager cm = (CurrencyManager) this.BindingContext[grid.DataSource, grid.DataMember]; PropertyDescriptorCollection pdc = cm.GetItemProperties(); string pkCol = "SSN"; //assumes Col1 is primary key column PropertyDescriptor pd = pdc.Find(pkCol, true); int colIndex = grid.Binder.NameToColIndex(pkCol); object val = grid[lastRowBeforeSorting, colIndex].Text; //primary key value on teh current row //Do the sort, assumes datasource is a DataTable DataTable dt = (DataTable) grid.DataSource; DataView dv = dt.DefaultView; GridBoundColumn gbColumn = grid.GridBoundColumns[col - 1]; string colName = gbColumn.MappingName; ListSortDirection sd = ListSortDirection.Ascending; if(grid[row, col].Tag != null) { sd = (ListSortDirection)grid[row, col].Tag; } string sdString = (sd == ListSortDirection.Ascending) ? "Desc" : "Asc"; dv.Sort = string.Format("{0} {1}", colName, sdString); //Do what you want to do after the sort, restore the current position //restore current object int rowIndex = (cm.List as IBindingList).Find(pd, val) + 1; colIndex = grid.CurrentCell.ColIndex; grid.CurrentCell.MoveTo(rowIndex, colIndex); grid.ScrollCellInView(rowIndex, colIndex); } }


AD Administrator Syncfusion Team April 4, 2005 04:01 AM UTC

However, how will we do if one grid has no any primary key? Please help! Thanks


DB David Bosak April 4, 2005 04:08 AM UTC

However, how will we do if one grid has no any primary key? Please help! Thanks


AD Administrator Syncfusion Team April 4, 2005 07:55 AM UTC

You will have to do somethiing to find the new current record. Without special information like a primary key, then worse case, you could loop through the DataRowView.Row''s in the DataView looking for the saved current DataRow. Note, you would not want to loop through the grid to do this. This will be slower as the grid has to get the data from the DataView. You can find the position of the saved DataRow in the DataView working only through the DataView, and then use grid.Binder.PositionToRowIndex to find the rowIndex in the grid. (If you use a GridGroupingControl instead of a GridDataBoundGrid, the current record will be moved through any sorts, and you do not have to do this additional work.)

Loader.
Up arrow icon