1
votes

I have a Windows Forms DataGridView that displays a list of students who are enrolled in a class. A combo box column in the DataGridView displays a list of alternative classes to which the student can be moved.

If a user selects a different class using the combo box, and then selects a different row in the DataGridView, the row that was just edited is removed from the DataGridView - correctly so, as it no longer meets the criteria.

My problem is that I need this to happen when the user selects the new value from the combo box, without waiting for the user to select another row. The reason is that if the row is not removed until the user selects another row, the rows move up to fill the gap left by the row that was removed, making the currently selected row the row below the one that the user selected, potentially confusing the user.

I've tried calling the Refresh method of the DataGridView, but the edited row still is not removed until the user selects another row.

2
Still looking for a solution to this. If i can't have the row that no longer meets the criteria removed immediately, before the user changes the selection, I'd happily settle for having it not removed until the user clicks the save button.Brendan Reynolds
You've only gotten 14 views at this time. You may get more with better tagging. Is this c# or VB.NET? Which .NET Framework version are you targeting?C-Pound Guru
Thanks. I've added the .net-4.0 tag, although I've no reason to think this is version-specific. I'll hold off on adding the C# tag for now, as I don't think the problem is language-specific and would be more than happy to get suggestions using VB.NET.Brendan Reynolds

2 Answers

0
votes

Try myGrid.EndEdit(); after the combo selected index changes to post the change to the grid.

You may also need to change the current cell to force the update:

DataGridViewCell currentCell = myGrid.CurrentCell;
try {             
    myGrid.EndEdit();
    myGrid.CurrentCell = null;
    myGrid.CurrentCell = currentCell;
}
catch {
    myGrid.CurrentCell = currentCell;
    myGrid.CurrentCell.Selected = true; 
}
0
votes

Ended up working around this as follows: instead of using the filtered DataView directly as the DataSource of the DataGridView, I used the DataView.ToTable() method to get a DataTable containing only the filtered records and used that as the DataSource of the DataGridView. As the DataGridView is no longer bound to the filtered DataView, the changed rows are no longer removed from the DataGridView when the selection changes. When the user clicks the 'Save' button, I merge the DataTable back into the original DataSet, save the changes, then call the DataView.ToTable() method again and re-bind the DataGridView to the DataTable to remove the changed records. Credit is due to my colleague Michael for the suggestion, thanks Michael.