[英]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. 我正在通过LINQ从SQL数据库中提取记录,并在两个单独的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. 例如:如果A,B和C存储在DGV1中。 And D, E, and F are stored in DGV2. D,E和F存储在DGV2中。 Only A, B, C, D, E, or F can be selected at a time. 一次只能选择A,B,C,D,E或F。
What I've tried so far: Using the SelectionChanged event for each DGV to call ClearSelection() in the other DGV. 到目前为止,我已经尝试过:对每个DGV使用SelectionChanged事件在另一个DGV中调用ClearSelection()。 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. 我也尝试过使用CellClick事件,但是它不能说明用户通过向上/向下键更改选择的情况。
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. 如果未选择任何内容,则不要在SelectionChanged
事件中为另一个网格调用ClearSelection()
。 ie If (changedGrid.SelectedRows.Count > 0) {OtherGrid.ClearSelection();}
即If (changedGrid.SelectedRows.Count > 0) {OtherGrid.ClearSelection();}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.