c# - NullReferenceException when searching for data in DataGridView -
hi i've written code search cardserial in gridview. error :
"object reference not set instance of object."
foreach (datagridviewrow row in datagridview2.rows) { if (row.cells["cardserial"].value.tostring().equals(textbox2.text)) { datagridview2.rows[row.index].defaultcellstyle.backcolor = color.yellow; } }
could tell me what's problem?
first check if value in cell not null (if calling tostring
on fails)
foreach (datagridviewrow row in datagridview2.rows) { var serial = row.cells["cardserial"].value; if (serial != null && serial.tostring().equals(textbox2.text)) { row.defaultcellstyle.backcolor = color.yellow; } }
Comments
Post a Comment