How do I figure out which items' selected state was changed in a DataGridView?
I have a DataGridView that has MultiSelect turned on. When the SelectionChanged event is fired, I'd like to know what items were newly selected and which items were newly deselected. For instance, if you have multiple items selected (by Ctrl-clicking) and then you release the Ctrl key and select a single item, I'd like to know 开发者_C百科which items were deselected. I could keep track of the previous selected items collection, but I just wanted to make sure I wasn't thinking too hard.
The event does not tell you exactly which things changed. If you need to know for some reason, you will have to keep track of the previous selection.
What are you trying to do in response to this event? There may be a much easier way to accomplish your real goal.
That information should be in the event arguments.
Use the RowStateChanged
event. the DataGridViewRowStateChangedEventArgs
will contain the row that was clicked. If a user selects/deselects multiple rows, the event will be called once for each row selected/deselected.
e.Row.Selected
will yield whether the row is now selected or deselected.
This information is not inherently available for a DataGridView
. However you could write a wrapper around the DataGridView
which provides this information.
public static void OnSelectionChanged(
this DataGridView view,
Action<List<DataGridViewRow>,List<DataGridViewRow>> handler) {
var oldSelection = view.SelecetedRows.Cast<DataGridViewRow>.ToList();
view.SelectedChanged += (sender,e) {
var newSelection = view.SelectedRows.Cast<DataGridViewRow>.ToList();
handler(oldSelection,newSelection);
oldSelection = newSelection;
};
}
Use Case
void HandleSelectionChanged(List<DataGridViewRow> oldRows, List<DataGridViewRow> newRows) {
..
}
void FormLoaded() {
myDataGridView.OnSelectionChanged(HandleSelectionChanged);
}
精彩评论