【发布时间】:2013-10-16 15:36:24
【问题描述】:
我有一个 Winforms 应用程序,其中几个事件可以触发以下代码。 . .
DataGridViewRow row = (DataGridViewRow)dataContactBusiness.CurrentCell.OwningRow;
int busID = (int)row.Cells["ID"].Value;
//...further processes utilising this ID
大多数情况下这是可以的,但有可能在用户选择 DataGridView 上的一行之前或什至在填充 DataGridView 之前触发此代码。发生这种情况时,我会收到异常“对象未设置为实例”等
什么是最好的测试方法
(a) DataGridView 有数据,并且 (b) 用户在该 DataGridView 中选择了一个单元格或行
【问题讨论】:
-
只需检查
CurrentCell是不是null? -
if (dataContactBusiness.CurrentCell != null) 成功了 - ty
标签: c# winforms datagridview