简体   繁体   中英

How to: Single Selection with Multiple DataGridViews

I am pulling records from a SQL database via LINQ and displaying the entries in two separate DataGridViews. I'm wondering what is the best way to allow only one row to be selected between the two components?

For example: If A, B, and C are stored in DGV1. And D, E, and F are stored in DGV2. Only A, B, C, D, E, or F can be selected at a time.

What I've tried so far: Using the SelectionChanged event for each DGV to call ClearSelection() in the other DGV. Obviously this was a horrible idea since it is essentially an infinite loop. I also tried playing around with the CellClick event but it would not account for users changing selection via up/down keys.

Any help is greatly appreciated and please let me know if any extra info is needed.

Don't call ClearSelection() for the other grid in the SelectionChanged event if nothing is selected. ie If (changedGrid.SelectedRows.Count > 0) {OtherGrid.ClearSelection();}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM