If I have a datagridview of say 7 Rows and 27 Columns and if I need to compare the values of cells with another cells how to do it?
Detail:
I need to compare 1st Row cell value with rest of the 6 rows cell values, same thing repeats for 2nd Row for rest 5 row, again same comparison needed for 3rd row with rest 4 rows. Basically It is row wise comparison
ROW1-> 2 2 2 4 4 6 6 8 8 8 7 7
ROW2-> 1 1 1 7 7 7 6 9 9 9 7 7
ROW3-> 1 1 3 3 3 3 5 5 5 0 0 0
ROW4-> 2 2 2 0 0 0 0 1 1 1 7 7
ROW5-> 0 0 0 3 3 3 4 4 4 8 8 8

Now I want to color those Cell whose values are repeating. Any idea will be a great help for me.

Here is the code

public void check_conflict()
        {
            int rowCount=dataGridView1.RowCount;
            int colCount=dataGridView1.ColumnCount;
            string val1;
            int c = 1;
            for (int k = 0; k < rowCount - 1; k++)
            {
                for (int i = 1; i < colCount; i++, c++)
                {
                    val1 = Convert.ToString(dataGridView1.Rows[k].Cells[i].Value);
                    for (int j = 1; j < rowCount - 1; j++)
                    {
                        string val2 = Convert.ToString(dataGridView1.Rows[j].Cells[i].Value);
                        if (val1 == val2 && (val1 != "" && val2 != ""))
                        {
                            DataGridViewCellStyle CellStyle = new DataGridViewCellStyle();
                            CellStyle.BackColor = Color.Red;
                            dataGridView1.Rows[j].Cells[i].Style = CellStyle;
                        }
                    }
                }
            }
        }

Problem I facing with code is it is only comparing 1 Row with others

how are you loading the data?

From DataSet here is the comlete code

       private void sun_Load(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[15];
                bool val = Convert.ToBoolean(chk.Value);
                if (val == true)
                {
                    //Console.WriteLine("TRUE " + chk.Value);
                }
                else
                {
                    //Console.WriteLine("FALSE " + chk.Value);
                }
            }
            aCommand1 = new OleDbCommand(query, main_connection);
            aAdapter1 = new OleDbDataAdapter(aCommand1);
            ds1 = new DataSet();
            aAdapter1.Fill(ds1, "app_info");
            int recCount = ds1.Tables[0].Rows.Count;

            dataGridView1.ColumnCount = 27;
            dataGridView1.Columns[0].Name = "Batch";
            dataGridView1.Columns[1].Name = "08:00";
            dataGridView1.Columns[2].Name = "08:30";
            dataGridView1.Columns[3].Name = "09:00";
            dataGridView1.Columns[4].Name = "09:30";
            dataGridView1.Columns[5].Name ="10:00";
            dataGridView1.Columns[6].Name ="10:30";
            dataGridView1.Columns[7].Name ="11:00";
            dataGridView1.Columns[8].Name ="11:30";
            dataGridView1.Columns[9].Name ="12:00";
            dataGridView1.Columns[10].Name ="12:30";
            dataGridView1.Columns[11].Name ="13:00";
            dataGridView1.Columns[12].Name ="13:30";
            dataGridView1.Columns[13].Name ="14:00";
            dataGridView1.Columns[14].Name ="14:30";
            dataGridView1.Columns[15].Name ="15:00";
            dataGridView1.Columns[16].Name ="15:30";
            dataGridView1.Columns[17].Name ="16:00";
            dataGridView1.Columns[18].Name ="16:30";
            dataGridView1.Columns[19].Name ="17:00";
            dataGridView1.Columns[20].Name ="17:30";
            dataGridView1.Columns[21].Name ="18:00";
            dataGridView1.Columns[22].Name ="18:30";
            dataGridView1.Columns[23].Name ="19:00";
            dataGridView1.Columns[24].Name ="19:30";
            dataGridView1.Columns[25].Name ="20:00";
            dataGridView1.Columns[26].Name ="20:30";

            for (int k = 0; k < recCount; k++)
            {
                int p_count = Convert.ToInt32(ds1.Tables[0].Rows[k][2].ToString());
                int col_count = 5;
                int teat = 4;
                string[] str = new string[p_count];
                string batchname = ds1.Tables[0].Rows[k][0].ToString();
                string[] teacher=new string[dataGridView1.ColumnCount];
                duration = double.Parse(ds1.Tables[0].Rows[k][3].ToString());
                dataGridView1.Rows.Add(batchname);
                for (int x = 1; x <= p_count; x++)
                {
                    for (int m = 1; m < dataGridView1.ColumnCount; m++)
                    {
                        string colname = dataGridView1.Columns[m].Name;
                        string tablecoltime = ds1.Tables[0].Rows[k][col_count].ToString();
                        if (colname == tablecoltime)
                        {
                            teacher[m] = ds1.Tables[0].Rows[k][teat].ToString();
                            dataGridView1.Rows[k].Cells[m].Value = ds1.Tables[0].Rows[k][teat].ToString();
                            if (duration == 1)
                            {
                                for (int dur = 1; dur <= 1; dur++)
                                {
                                    dataGridView1.Rows[k].Cells[m+dur].Value = ds1.Tables[0].Rows[k][teat].ToString();
                                }
                            }
                            else if (duration == 1.3)
                            {
                                for (int dur = 1; dur <= 2; dur++)
                                {
                                    dataGridView1.Rows[k].Cells[m + dur].Value = ds1.Tables[0].Rows[k][teat].ToString();
                                }
                            }
                            else if (duration == 2)
                            {
                                for (int dur = 1; dur <= 3; dur++)
                                {
                                    dataGridView1.Rows[k].Cells[m + dur].Value = ds1.Tables[0].Rows[k][teat].ToString();
                                }
                            }
                        }
                        else
                        {
                            teacher[m] = "";
                        }
                    }
                    teat += 2; col_count += 2;
                }
            }
            dataGridView1.Columns[0].ReadOnly = true;
            check_conflict();
        }
        string query = null;
        public void sun_batc(string[] batch_info)
        {
            int arr_len=batch_info.Length;
            string condition = null;
            for (int i = 0; i < arr_len; i++)
            {
                condition += "branch Like '" + batch_info[i] + "' OR ";
            }
            condition=condition.Substring(0,condition.Length - 3);
            query = "select * from weekly where bday like 'Sun' AND (" + condition +")";
        }
        public void check_conflict()
        {
            int rowCount=dataGridView1.RowCount;
            int colCount=dataGridView1.ColumnCount;
            string val1;
            int c = 1;
            for (int k = 0; k < rowCount - 1; k++)
            {
                for (int i = 1; i < colCount; i++, c++)
                {
                    val1 = Convert.ToString(dataGridView1.Rows[k].Cells[i].Value);
                    for (int j = 1; j < rowCount - 1; j++)
                    {
                        string val2 = Convert.ToString(dataGridView1.Rows[j].Cells[i].Value);
                        if (val1 == val2 && (val1 != "" && val2 != ""))
                        {
                            DataGridViewCellStyle CellStyle = new DataGridViewCellStyle();
                            CellStyle.BackColor = Color.Red;
                            dataGridView1.Rows[j].Cells[i].Style = CellStyle;
                        }
                    }
                }
            }
        }

If I am a cell and if the cell right of me has the same value, I should color myself.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.