【问题标题】:Testing DataGridView to see if Cell has been Selected测试 DataGridView 以查看是否已选择单元格
【发布时间】: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


【解决方案1】:

if (dataContactBusiness.CurrentCell != null) 成功了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-04
    • 2017-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-16
    • 2015-06-27
    相关资源
    最近更新 更多