0

我正在使用 datagridview 上的一行中的值来构建图像路径以传递给图片框。

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
    DataGridViewRow currentRow = new DataGridViewRow();
    currentRow = dataGridView1.CurrentRow;
    string valueJob = currentRow.Cells[2].Value.ToString();
    string valueBatch = currentRow.Cells[3].Value.ToString();
    string valueImmagine = currentRow.Cells[4].Value.ToString();
    string result = Octopus2.Properties.Settings.Default.DataPath + "\\" + valueJob + "\\" + valueBatch + "\\" + valueImmagine + ".jpg";   
    pictbox.ImageLocation = result;
}

问题是,当我使用另一个控件执行 DataSet.clear() 时,代码返回以下错误:“NullreferenceException 未被用户代码处理”。

提前感谢您,任何帮助将不胜感激。

4

3 回答 3

1

尽管我无法在您显示的代码中设置 DataSet.clear() 但如果 DataSet.clear() 给出此错误,则 DataSet 为空。

于 2010-02-26T12:51:50.530 回答
1

正如我之前提到的,DataSet.clear() 由另一个由控件调用的事件处理程序调用,即重置按钮。我通过这样做解决了这个问题:

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
    if (dataGridView1.RowCount >=1)
    {
        DataGridViewRow currentRow = new DataGridViewRow();
        currentRow = dataGridView1.CurrentRow;
        string valueJob = currentRow.Cells[2].Value.ToString();
        string valueBatch = currentRow.Cells[3].Value.ToString();
        string valueImmagine = currentRow.Cells[4].Value.ToString();

        string result = Octopus2.Properties.Settings.Default.DataPath + "\\" + valueJob + "\\" + valueBatch + "\\" + valueImmagine + ".jpg";

        pictboxImpegnativa.ImageLocation = result;
    }
    else
    {
        MessageBox.Show("good work.");
    }
}

感谢您的时间和帮助。

于 2010-02-26T21:41:18.227 回答
0

您忘记检查是否currentRow.Cells[x].Value也可以为空。您应该添加另一个如下所示的代码:

if (currentRow.Cells[n].Value != null) 
{
    // your code here
}

相信我,你需要为空值捕获一个内部对象,而不仅仅是来自DataGridViewRow.

于 2010-10-21T13:47:08.637 回答