Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 work after

C#
if (row.Cells["Status"].Value.ToString() == "Otvoren" && row.Cells["RN"].Value.ToString() == null)


What I have tried:

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");
                }

            }
        }
Posted
Updated 7-Oct-22 10:37am
v2

1 solution

It's not entirely clear, but you seem to be saying that adding
C#
row.Cells["RN"].Value.ToString() == null
causes a problem. By "don't work" I am taking it to mean that you get an exception raised when you run the program and that cell is null.

I believe it is because you are trying to execute the .ToString() method on a null instance - which will cause an error.

Instead of using .ToString, test for the actual value being null i.e.
C#
row.Cells["RN"].Value == null
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900