C# DataSet.clear()上的NullreferenceException问题
我正在使用datagridview上的一行中的值来构建要传递到picturebox的图像路径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
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()与您的代码之间的链接是什么?可能存在重复的