Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Datagridview compare cell string

I need to compare 2 String-Values in one cell.(if in cell 1 is a string like ‘DPS’ and ‘MAN’)
But I dont know why my && Operator does not work like i think. I dont get an Result.

enter image description here

Can someone explain what I am doing wrong here?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

private void Warning()
{
    foreach (DataGridViewRow row in dataGridView4.Rows)
        if (row.Cells[1].Value.ToString() == "DPS" 
            && row.Cells[1].Value.ToString() == "MAN")
        {
            panelWarnung.BackColor = Color.Red;
        }
        else
        {
            panelWarning.BackColor = Color.LightGreen;
        }
}

SOLUTION

This works!

foreach (DataGridViewRow row in dataGridView4.Rows)
            if (row.Cells[1].Value.ToString().Contains("DPS") == row.Cells[1].Value.ToString().Contains("MAN"))
                panelWarning.BackColor = Color.Red;

>Solution :

if DPS and MAN is in the cell the warningPanel should turn red

That’s

row.Cells[1].Value.ToString().Contains("DPS") 
    && row.Cells[1].Value.ToString().Contains("MAN")

Contains tests whether the string is present somewhere within the cell. == tests whether the entire whole string in the cell is exactly equal to some value.. And no variable can be simultaneously equal to two different values

Contains will permit you to have cell values like:

MANDPS
DPS,MAN
DPSINGH'S MANAGER

🙂

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading