【发布时间】:2014-10-21 02:51:08
【问题描述】:
我希望它能够接受将 null 作为返回值,但是在提取不存在的数据时,我不断收到 nullexception 错误……是的,我明白这听起来如何。
基本上,如果在某一列中没有找到数据,则创建一行。
获取空错误 where 子句,因为它找不到值.... 我如何解决这个问题并允许“where”在其行不给出错误?
var row = RGV.Rows.Cast<DataGridViewRow>()
.Where(r => r.Cells["firstcolumn"].Value.ToString() == please)
.First();
if (row != null)
{
rowIndex = row.Index;
RGV.Rows[rowIndex].Cells[secondcolumn].Value = help;
RGV.Refresh();
}
else
{
RGV.Rows.Add();
int indexNew = RGV.Rows.Count;
RGV.Rows[indexNew].Cells["firstcolumn"].Value = please;
RGV.Rows[indexNew].Cells["secondcolumn"].Value = help;
RGV.Refresh();
}
【问题讨论】:
-
RGV.Rows可能是Nothing? -
用默认值替换空值
-
这意味着
r.Cells["firstcolumn"].Value是Nothing并且Nothing上的ToString()会引发异常。尝试使用RowsAdded事件用默认值填充行。即使是一个空白字符串也可以。
标签: c# linq datagridview