Issue with sorted Datagrid going to previous rec

blackduck603

Member
Joined
Sep 17, 2008
Messages
21
Location
NH
Programming Experience
3-5
When my Datagrid is sorted by a column, for some reason it jumps to the previously selected row when I click on a different row.

I added some debug.writeline calls in the CurrentCellChanged event handler and the CurrentCell properties show the row as the previously selected (not the row that I just clicked).

Example:
Datagrid is sorted by status field

ID DESC STATUS
5 5TEXT A
7 7TEXT A
8 8TEXT B
11 11TEXT B
15 15TEXT C

Select row with ID = 7 in Datagrid.
this is row 2 (using 1 based numbering)
change record so status changes to C
this should move ID 7 to row 5
ID 7 does not move at this point

when I click on th erow with ID 8
now ID 7 moves to row 5 and that becomes the selected row.

I cannot figure out why the click on a different row in the datagrid is not working when the data grid is in sort mode.

I appreciate any assistance with this.

Thanks
 
Each cell in the grid has a Value property. When you start editing a cell a control is embedded in that cell and populated from the cell's Value. As you edit, the contents of the embedded control changes but the Value of the cell doesn't. If you press the Escape key then the embedded control is removed and the cell's Value never gets changed. If you leave the cell while editing, it is at that point that the contents of embedded control is written back to the cell's Value property.

In your case, you change a Status value but the cell's Value hasn't changed because the cell is still being edited. When you click on another row, it's at that point that the cell's Value is updated and the data resorted. The row you clicked will then be selected but it may not contain the data that it did before the resort occurred.
 
jmcilhinney,

That makes sense. The strange part is the behavior is not quite how you described and how I would expect. when the next row is clicked, the datagird list gets resorted, but the selected cell is the one that was edited - not the one that was just clicked.

The desired behavior (from the users) is to have a checkbox option on the form which will either allow the behavior I just described (follow the modified cell) or to force the datagrid to the original cell row - which now will contain a different record (like you described). I modified the code and have this working.

VB.NET:
                iRow = Me.dgActionForms.CurrentCell.RowNumber  
                ' ACTION FORM 002526 PART B:  Sorted Datagrid Behavior Issue
                If Me.mnuOptionDgSortOverride.Checked = True Then
                    ' Force the datagrid to stay on the current row
                    ' do NOT follow the modified record
                    dgActionForms.CurrentRowIndex = iRow 
                    iRow = Me.dgActionForms.CurrentCell.RowNumber  '
                     dgActionForms.Select(iRow)
                    dgActionForms.Refresh()
                    ' Get data for this record into other form controls
                    LoadProbno()
                Else
                    ' Follow the modified record
                    dgActionForms.Select()
                End If

thanks
 
Back
Top