简体   繁体   English

测试DataGridView以查看是否已选择Cell

[英]Testing DataGridView to see if Cell has been Selected

I have a Winforms application where several events can trigger the following code . 我有一个Winforms应用程序,其中几个事件可以触发以下代码。 . .

DataGridViewRow row = (DataGridViewRow)dataContactBusiness.CurrentCell.OwningRow;
int busID = (int)row.Cells["ID"].Value;
//...further processes utilising this ID

Most of the time this is OK, but it is possible for this code to be triggered before the User has selected a row on the DataGridView or even before the DataGridView is populated. 大多数情况下这是可以的,但是可以在用户在DataGridView上选择行之前或甚至在填充DataGridView之前触发此代码。 When this happens I get an exception 'object not set to an instance' etc 当发生这种情况时,我得到一个异常'对象未设置为实例'等

What is the best way to test to see if 测试是否最好的方法是什么?

(a) The DataGridView has data, AND (b) The User has selected a cell or row in that DataGridView (a)DataGridView具有数据,AND(b)用户在该DataGridView中选择了一个单元格或行

if(dataContactBusiness.CurrentCell!= null)完成了这个伎俩

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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