将发布变量发布到组合框控件文本值

时间:2012-04-06 13:51:23

标签: c# winforms datagrid

我在winforms应用程序上有一个数据网格,当用户双击数据网格上的一个单元格时,将调用以下方法。

private void dataCaseDiary_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        try
        {
            int cellRow = e.RowIndex;
            int cellCol = e.ColumnIndex;
            DataTable table = (DataTable)dataCaseDiary.DataSource;
            var currentDiaryID = table.Rows[cellRow]["DiaryID"];
            var currentUser = table.Rows[cellRow]["To Action"];
            var currentType = table.Rows[cellRow]["Diary Type"];
            txtDiaryID.Text = currentDiaryID.ToString();
            cboReassign.Text = currentUser.ToString();
            cboAmendDiaryType.Text = currentType.ToString();
        }
        catch (Exception eX)
        {
            MessageBox.Show(eX.Message);
        }
    }

然而,行

发生异常
cboAmendDiaryType.Text = currentType.ToString();

该异常指出“对象引用未设置为对象的实例”。

我花了一些时间看这个,但找不到它我做错了什么。三个var变量都从datagrid中获取正确的值,其中两个发布到表单上控件的文本值,但第三个失败。

我使用'txt'的前缀表示文本框控件,'cbo'表示组合框。

1 个答案:

答案 0 :(得分:0)

问题是table.Rows[cellRow]["Diary Type"]不存在并返回null。我猜“日记类型”不是正确的列名。

相关问题