Goran Bibic

Goran Bibic

  • 448
  • 2.9k
  • 175.3k

Search datagrid if value is null or no data C#

Mar 29 2023 10:37 AM

Hello i have simple problem
If no values in datagrid need to repair my code

Error is:

System.NullReferenceException: 'Object reference not set to an instance of an object.' System.Windows.Forms.DataGridViewCell.Value.get returned null.

My code

private void ListDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    foreach (DataGridViewRow row in ListDataGridView.Rows)
    {
        if (String.IsNullOrWhiteSpace(row.Cells["R.b."].Value.ToString()))
        {
            row.DefaultCellStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#51B3E8");
        }
        else
        {
            row.DefaultCellStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#09ECA6");
        }
    }
}

 


Answers (2)