I put two condition from two cells but don't wotk, please respond
First part condition work fine, when put && and other condition don't wotk
C#
if (row.Cells["Status"].Value.ToString() == "Otvoren" && row.Cells["RN"].Value.ToString() == null)Line 6
C#
private void OrdersDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
foreach (DataGridViewRow row in OrdersDataGridView.Rows)
{
if (row.Cells["Status"].Value.ToString() == "Otvoren" && row.Cells["RN"].Value.ToString() == null)
{
row.DefaultCellStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#FF5F1F");
}
}
}
Rajanikant HawaldarRank #110 · 20.2K reputationPosted Oct 7, 2022, 5:09 AM EDT
private void OrdersDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { foreach (DataGridViewRow row in OrdersDataGridView.Rows) { if (row.Cells["Status"].Value.ToString() == "Otvoren" && string.IsNullOrEmpty(Convert.ToString(row.Cells["RN"].Value))) { row.DefaultCellStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#FF5F1F"); } } }Uday DodiyaRank #239 · 10.3K reputationPosted Oct 7, 2022, 5:07 AM EDT
You can try IsNullOrEmpty function to check empty or Null Values in string
private void OrdersDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { foreach (DataGridViewRow row in OrdersDataGridView.Rows) { if (row.Cells["Status"].Value.ToString() == "Otvoren" && String.IsNullOrEmpty(row.Cells["RN"].Value.ToString())) { row.DefaultCellStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#FF5F1F"); } } }Rajanikant HawaldarRank #110 · 20.2K reputationPosted Oct 7, 2022, 5:11 AM EDT
@Uday, if Value is null ToString() will through runtime exception