你调用的对象是空的。从gridView中选择任何行时出现

时间:2013-10-04 17:00:42

标签: c# winforms datagridview nullreferenceexception

我有2个表单.. gridForm有一个dataGridView从数据库获取数据,第二个表单editFormtextboxes类似于gridView Columns

我想在grid中选择一行,然后点击修改按钮,必须显示editForm,文本框中包含网格值

gridForm

private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex >= 0)
        {
            DataGridViewRow row = dataGridView1.Rows[e.RowIndex];

            // when i put this line only , it works right.
            edditContactForm.edditContactNameSetter = row.Cells["contactNameGridViewColumn"].Value.ToString();

            // this also with the previous is working right too.
            edditContactForm.edditJobTitleSetter = row.Cells["jobTitleGridViewColumn"].Value.ToString();

            // the problem appears here and the exhibition shown to this line and any similar lines under it. 
            edditContactForm.edditCompanyNameSetter = row.Cells["CompanyNameGridViewColumn"].Value.ToString();
        }
    }

editForm:

    public string edditContactNameSetter 
    {
        set { txtContactName.Text = value; }
    }

    public string edditJobTitleSetter
    {
        set { txtJobTitle.Text = value; }
    }

    public string edditCompanyNameSetter
    {
        set { txtCompanyName.Text = value; }
    }

当我从网格中选择任何一行时,展览会出现 enter image description here

1 个答案:

答案 0 :(得分:0)

好的,我发现了问题,因为我是c#的新手,我觉得这对专业人士来说是一个天真的错误。

问题出在SELECT FROM查询中,我只是没有从数据库中选择companyName值,因此row.Cells["CompanyNameGridViewColumn"].Value得到null