Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DataSet.clear()上的NullreferenceException问题_C#_Datagridview_Nullreferenceexception - Fatal编程技术网

C# DataSet.clear()上的NullreferenceException问题

C# DataSet.clear()上的NullreferenceException问题,c#,datagridview,nullreferenceexception,C#,Datagridview,Nullreferenceexception,我正在使用datagridview上的一行中的值来构建要传递到picturebox的图像路径 private void dataGridView1_SelectionChanged(object sender, EventArgs e) { DataGridViewRow currentRow = new DataGridViewRow(); currentRow = dataGridView1.CurrentRow; string valueJob = currentR

我正在使用datagridview上的一行中的值来构建要传递到picturebox的图像路径

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”


提前感谢您,我们将非常感谢您的帮助。

虽然我无法在您显示的代码中使用DataSet.clear(),但如果DataSet.clear()出现此错误,则DataSet为空。

如前所述,DataSet.clear()由控件调用的另一个eventhandler调用,即重置按钮。 我通过这样做解决了问题:

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.");
    }
}

感谢您的时间和帮助。

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

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

相信我,有一个内部对象需要捕捉为null,而不仅仅是来自
DataGridViewRow
的对象

此代码是否引发异常或Clear()?DataSet.Clear()与您的代码之间的链接是什么?可能存在重复的