Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
Posted
Comments
kumar2413 5-Jun-13 2:16am    
Post your sql statement to work out.for the value not repeating use DISTINCT and try.
vishal deb 5-Jun-13 2:21am    
Thank You Kumar but I want to view this table with full data I don't want to use DISTINCT.
Here is the C# code of winform
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
lukeer 5-Jun-13 2:48am    
Place this code in your question. Use the "Improve question" link for that and enclose code in <pre lang="c#">YourCodeHere();</pre> tags to make it easily readable.
kumar2413 5-Jun-13 2:35am    
What you should actually compare?
vishal deb 5-Jun-13 2:46am    
You can see the table in my post where I have made everything clear
Take Example No. 1. ROW1 and ROW4 is having same values that is 2 2 2 again ROW1 and ROW2 have same value 6 and again ROW1 and ROW2 and ROW4 have same value 7 7 7 so now I want to color them with RED color as showing the conflicts.
Take Example No. 2. ROW2 and ROW3 have same value 1 1 so I want to color them in RED to show the conflict.
I hope now my intention is clear

1 solution

C#
for(int comparerRow = 0; comparerRow < rowCount - 1; comparerRow++)
{
    for(int compareeRow = comparerRow + 1; compareeRow < rowCount; compareeRow++)
    {
        for(int columnIndex = 0; columnIndex < columnCount; columnIndex++)
        {
            // Comparison and visual effect
        }
    }
}

The important part is that the inner row loop has to depend on the outer row loop's iteration variable. So it won't start from the beginning over and over again.
 
Share this answer
 
Comments
vishal deb 5-Jun-13 3:20am    
Thank You Lukker You are right
I was confuesd with the loops
lukeer 5-Jun-13 3:41am    
I'm glad that I could help.

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