Keep selection in a datagrid in sync after a sort

  • Thread starter Thread starter Iain
  • Start date Start date
I

Iain

I want to select whole rows in my Datagrid. I do so using code from
SyncFusions WinForms FAQ which overrides the ColumnStyle Edit command and
deselects and selects according to a private variable as in the code sample
below.

The problem is if I click on a header to sort the DataGrid the results are
not what I want.

Basically, I need a way to detect that the sort has occured (before and
after) so I can work out what new rows to select and deselect.

This seems impossible. Are there any workarounds?
Iain



protected override void Edit(System.Windows.Forms.CurrencyManager source,
int rowNum, System.Drawing.Rectangle bounds,
bool readOnly, string instantText,bool cellIsVisible)
{
if (mWholeRows)
{
if(mSelectedRow > -1 && mSelectedRow < source.List.Count + 1)
this.DataGridTableStyle.DataGrid.UnSelect(mSelectedRow);
mSelectedRow = rowNum;
this.DataGridTableStyle.DataGrid.Select(mSelectedRow);
}
else
base.Edit(source, rowNum, bounds, readOnly, instantText, cellIsVisible);
}
 
Hi,

The general approach is:

a) To override the grid's OnMouseDown protected method to trap clicks on
column headers (the HitTest method can be used to determine where exactly
the click occured).
b) Store the primary key of the current row in a private variable
c) In the OnMouseUp event, after calling base.OnMouseUp(), look up the
stored primary key value and select the appropriate row in the grid.
 
Back
Top