none
How to highlight rows in datagriedview that not in datatable rows in c# RRS feed

  • Question

  • I have a datagriedview contains many rows of numbers.

    And i have a datatable with one column contains numbers.

    I want to highlight datagrideview rows that not in datatable rows .. I compare between just one column

    Here is my code:

       DataTable SeatNum = new DataTable()
    SeatNum = ......
                try
                {
                    foreach (DataGridViewRow row in dataGridView2.Rows)
                        {
                            if (row.IsNewRow) { return; }
                    foreach (DataRow dtrow in SeatNum.Rows)
                            {
                    if (dtrow[0].ToString() != (row.Cells[0].Value.ToString()))
                                    
    {
    row.Cells[0].Style.BackColor = Color.Red;
    MessageBox.Show("Not Exist" + row.Cells[0].Value.ToString() + "\r\n" + dtrow[0].ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                break;
    }
         else
    {
    row.Cells[0].Style.BackColor = row.DefaultCellStyle.BackColor;
    MessageBox.Show( "Exist" + row.Cells[0].Value.ToString() + "\r\n" + dtrow[0].ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                break;
    }                     }
                    }
    }
    catch (Exception exc)
                    {
                        MessageBox.Show(exc.Message);
                    }
    This code just highlight the rows that not match to the first row in datatable

    Thank you in advance.

    Monday, June 24, 2019 4:18 AM

All replies

  • Hi Abohy,

    Thank you for posting here.

    Based on your description, you want to highlight datagrideview rows that not in datatable rows.

    I make a simple code for your reference.

    private void Button1_Click(object sender, EventArgs e)
            {
                DataTable table = new DataTable();
                table.Columns.Add("Count", typeof(int));
                table.Rows.Add(10);
                table.Rows.Add(20);
                table.Rows.Add(15);
                table.Rows.Add(19);
                table.Rows.Add(310);
                table.Rows.Add(79);
                DataTable dataTable = (DataTable)dataGridView1.DataSource;
                DataRowCollection dr = dataTable.Rows;
                List<int> list = new List<int>();
                for (int i = 0; i < dr.Count; i++)
                {
                    list.Add(Convert.ToInt32(dr[i]["Num"]));
                }
                DataRowCollection dr1 = table.Rows;
                List<int> list1 = new List<int>();
                for (int i = 0; i < dr1.Count; i++)
                {
                    list1.Add(Convert.ToInt32(dr1[i]["Count"]));
                }
                var result = list.Where(p => !list1.Any(p2 => p2 == p)).ToList();
                for (int i = 0; i < dr.Count; i++)
                {
                    foreach (var item in result)
                    {
                        if(Convert.ToInt32(dr[i]["Num"])==item)
                        {
                            dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                        }
                    }
                }
               
    
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("Num", typeof(int));
                dt.Rows.Add(10);
                dt.Rows.Add(20);
                dt.Rows.Add(15);
                dt.Rows.Add(332);
                dt.Rows.Add(310);
                dt.Rows.Add(78);
                this.dataGridView1.DataSource = dt;
            }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 24, 2019 7:15 AM
    Moderator