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

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -