Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
http://www.2shared.com/photo/tvmgAetg/eg12.html[^]

There are two panels in the picture. Panel 1 have 6 rows and 1 column. In panel 1 there are 2 inner rows stated as DL and UL. In panel 2 there are 3 inner rows.

I need to search the values in datagrid from panel 1 and compare with the values from panel 2 datagrids and if they are similar in values, mark both location with similar color.
Posted

Here's a pseudo-code solution just to give you a simple algorithm, which should put you on the right lines. The actual re-colouring bit I can't remember the code for off the top of my head so take the essence of what it is doing rather than the actual code. If you're still stuck, then please give some more details and your own code so far.


C#
// Assuming you have already read out your Grid values into two lists, listAValues and listBValues...


// Step 1 - Get distinct lists
List<int> distinctMatchingValues = (from a in listAValues.Distinct()
                                    join b in listBValues.Distinct() on a equals b
                                    select a).ToList();


// Step 2 - Give each distinct value a random colour
Dictionary<int, Color> palette = new Dictionary<int, Color>();
foreach(var i in distinctMatchingValues)
{
    palette.Add(i, GetNextOrRandomColor()); //Add whatever function you like here to give you a different colour each time
}


// Step 3 - Iterate through each cell and colour it according to the dictionary
foreach(var column in gridAColumns)
{
    foreach(var cell in column)
    {
       cell.Style = new DataGridViewStyle{ BackColor = palette[cell.Value] };
    }
}
foreach(var column in gridBColumns)
{
    foreach(var cell in column)
    {
       cell.Style = new DataGridViewStyle{ BackColor = palette[cell.Value] };
    }
}
 
Share this answer
 
While compiling this code, I am getting this error: Can anyone have an idea why I am getting this error ?


"Cannot implicitly convert type 'System.Collections.Generic.List<string>' to 'System.Collections.Generic.List<int>'"


"foreach statement cannot operate on variables of type 'System.Windows.Forms.DataGridView' because 'System.Windows.Forms.DataGridView' does not contain a public definition for 'GetEnumerator'"




C#
{
    rows = 0;
    string[] listBValues = new string[16];

    for (int col = 0; col < dataGridView25.Rows[rows].Cells.Count; col++)
    {
        if (dataGridView25.Rows[rows].Cells[col].Value.ToString() != "")
        {
            listBValues[pos] = dataGridView25.Rows[rows].Cells[col].Value.ToString();
            pos++;
        }
    }



    /// Step 1 - Get distinct lists

    List<int> distinctMatchingValues = (from a in listAValues.Distinct()
                                        join b in listBValues.Distinct() on a equals b
                                        select a).ToList();


    // Step 2 - Give each distinct value a random colour
    Dictionary<int, Color> palette = new Dictionary<int, Color>();
    foreach (var i in distinctMatchingValues)
    {
        //palette.Add(i, GetNextOrRandomColor()); //Add whatever function you like here to give you a different colour each time
        var color = Color.FromArgb(randomGen.Next(255), randomGen.Next(255), randomGen.Next(255));
    }


    // Step 3 - Iterate through each cell and colour it according to the dictionary
    foreach (var column in dataGridView1)
    {
        foreach (var cell in column)
        {
            cell.Style = new DataGridViewStyle { BackColor = palette[cell.Value] };
        }
    }
    foreach (var column in dataGridView25)
    {
        foreach (var cell in column)
        {
            cell.Style = new DataGridViewStyle { BackColor = palette[cell.Value] };
        }
    }

}
 
Share this answer
 
Comments
Adam David Hill 21-Jun-12 12:24pm    
I think you need something like dataGridView1.Columns rather than just dataGridView1. You'd have to explore the properties of DataGridView. It may be that it's better to iterate via the rows rather than the columns - the heart of the solution provided was that however the control likes you to do it, you essentially want to iterate all the cells.

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