Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
请问这里有什么问题
foreach (DataGridViewRow row in frm02.dgv02.Rows) { foreach (DataGridViewCell cell in row.Cells) { if (cell.Value.ToString() == "323") //if (cell.RowIndex == 3) { cell.Selected = true;
我得到了 NullReferenceException
如果我用第二个替换第一个 - 它可以工作。
您正在访问的单元格中的值似乎包含一个空值。然后,您对该空值调用 .ToString() 。在尝试对该值调用 .ToString() 之前,您可以进行空检查。
你会这样做:
if (cell.Value != null && cell.Value.ToString() == "323")