c# - Check if datagridview cell is null or empty -
this question has answer here:
- what nullreferenceexception, , how fix it? 29 answers
i have change background color of cells , when value string or empty, code have write similar other codes here :
for (int rowindex = 0; rowindex < datagridview1.rowcount; rowindex++) { string conte = datagridview1.rows[rowindex].cells[7].value.tostring() ; if (string.isnullorempty(conte)) { // datagridview1.rows[rowindex].cells[7].style.backcolor = color.orange; } else { datagridview1.rows[rowindex].cells[7].style.backcolor = color.orange; } } the dataset complete , show me datagridview populated , show me error: 
how can fix this?? have way write code?
i iterate through cells using following..
foreach (datagridviewrow dgrow in datagridview1.rows) { var cell = dgrow.cells[7]; if (cell.value != null) //check null reference { cell.style.backcolor = string.isnullorempty(cell.value.tostring()) ? color.lightcyan : color.orange; } }
Comments
Post a Comment