如何在不同的dataGrids中找到相同的值? [英] How to find the same values in different dataGrids?

查看:67
本文介绍了如何在不同的dataGrids中找到相同的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://www.2shared.com/photo/tvmgAetg/eg12.html [ ^ ]

图片中有两个面板.面板1有6行和1列.在面板1中,有2个内部行分别表示为DL和UL.在面板2中有3个内部行.

我需要从面板1的datagrid中搜索值,并与面板2的数据网格中的值进行比较,如果它们的值相似,则用相似的颜色标记两个位置.

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.

推荐答案

这里是一个伪代码解决方案,只是为您提供了一种简单的算法,该算法应使您处于正确的位置.实际的重新着色位使我不记得我头上的代码,因此请充分利用其工作内容,而不是实际的代码.如果仍然遇到问题,请提供更多详细信息和您自己的代码.


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.


// 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] };
    }
}


在编译此代码时,我收到此错误:有人能知道为什么我收到此错误吗?


无法将类型"System.Collections.Generic.List< string>"隐式转换为"System.Collections.Generic.List< int>"''"


"foreach语句无法对类型为"System.Windows.Forms.DataGridView"的变量进行操作,因为"System.Windows.Forms.DataGridView"不包含"GetEnumerator"的公共定义"




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''"




{
    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] };
        }
    }

}


这篇关于如何在不同的dataGrids中找到相同的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆