如何以特定方式对数据网格视图进行分类和排列? [英] How to categorize and arrange a datagridview in a specific manner?

查看:60
本文介绍了如何以特定方式对数据网格视图进行分类和排列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我有winform c#2012

我已经定义了一个datagridview,其中有一个值。我的datagridview有2列(绿色和红色),如下所示:

------------------------

GREEN | RED | // daatagridview的标题

-----------------------

A | B |

-----------------------

C | A |

-----------------------

D | D |

-----------------------

E | Z |

-----------------------

E | Z |

-----------------------

我想做的是安排datagridview使得

- 两列中出现的所有项目都从GREEN列中删除

- 在同一列中出现多次的每个项目,只保留一个,其余的重复项被删除(即:同一列中的重复数据被删除)



所以上面的datagridview应该在转换后变成这样:

------------------------

GREEN | RED | // daatagridview的标题

-----------------------

E | B |

-----------------------

C | A |

-----------------------

E | D |

-----------------------

| Z |

------------------------



我试过的确有效吗

我该怎么办?

提前谢谢



我尝试过:



hi all, i have winform c# 2012
i have defined a datagridview in which there is a values. My datagridview has 2 columns (GREEN and RED ) and looks as follow:
------------------------
GREEN | RED | //headers of daatagridview
-----------------------
A | B |
-----------------------
C | A |
-----------------------
D | D |
-----------------------
E | Z |
-----------------------
E | Z |
-----------------------
what i want to do is to arrange the datagridview such that
-every item that appears in both columns is removed from GREEN column
-every item that appears more than one time in a same column, only one is kept ,the rest of duplicates is removed (ie: duplicate data in the same column are removed)

so the above datagridview should become after transformation like this:
------------------------
GREEN | RED | //headers of daatagridview
-----------------------
E | B |
-----------------------
C | A |
-----------------------
E | D |
-----------------------
| Z |
------------------------

what i have tried does work
how can i do it?
thanks in advance

What I have tried:

List<string> list = new List<string>();
           
          
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
              
                if (dataGridView1.Rows[i].Cells[0].Value != null)
               {
                   string str_green = dataGridView1.Rows[i].Cells[0].Value.ToString();
                  if (!list.Contains(str_green))
                  {
                       list.Add(str_green);
                    }
                    else
                    {
                       dataGridView1.Rows.Remove(dataGridView1.Rows[i]);
                       
                   }
                }
               if (dataGridView1.Rows[i].Cells[1].Value != null)
               {
                 string str_red = dataGridView1.Rows[i].Cells[1].Value.ToString();
                    if (!list.Contains(str_red))
                    {
                       list.Add(str_red);

                    }
                    else
                    {
                        dataGridView1.Rows.Remove(dataGridView1.Rows[i]);
                       
                   }
                }
               
            //}

推荐答案

如何在特定列的特定单元格中添加数据 [ ^ ] ...

这些是我在填充datagridview之前解决的问题 - 实际上我可能会在填充原始DataTable时解决这些问题。



因此对其他帖子的解决方案的调整是这个(或类似的)

Going back to my solution to your earlier question at How to add data in a specific cell from specific column[^] ...
These are issues I would address before populating the datagridview - in fact I would probably address the problems as I was populating the original DataTable.

So the adjustment to the solution on the other post is this (or similar)
//get the "red" items first (because if they appear in both we don't want them in the green list)
var redItems = (dt.AsEnumerable().Where(x => x.Field<string>("Flag_created") == "Red")
    .Select(x => x.Field<string>("Society_Id"))).Distinct().ToList();
int r = redItems.Count();

//Get the "green" items
var greenItems = (dt.AsEnumerable()
    .Where(x => x.Field<string>("Flag_created") == "Green")
    .Select(x => x.Field<string>("Society_Id"))).Except(redItems).Distinct().ToList();
int g = greenItems.Count();

// Whichever is the greater count of items, add that many rows to the grid
dataGridView1.Rows.Add(Math.Max(g, r));

for (var i = 0; i < dataGridView1.Rows.Count; i++)
{
    if (i < greenItems.Count)
        dataGridView1.Rows[i].Cells[greenColumn].Value = greenItems[i];
    if (i < redItems.Count)
        dataGridView1.Rows[i].Cells[redColumn].Value = redItems[i];
}



注意事项:

- redItems和greenItems现在 List< string> 不是列表< datagridviewrow>

- 我首先得到redItems因为我想从greenItems中排除任何一个 - 请参阅。除了 [ ^ ]

- 我只想要唯一的值,所以请注意 .Distinct [ ^ ]

- 还要注意我从以前的解决方案填充dataGridView的方式不同(因为列表项现在是字符串)



为了完整性,如果已经填充了DataGridView,则可以将列转换为Lists t o稍后进行此操作...我创建了一个按钮来删除这样的重复项


Things to note:
- redItems and greenItems are now List<string> not List<datagridviewrow>
- I get the redItems first because I want to exclude any of those from the greenItems - see the .Except[^]
- I only want unique values so note the .Distinct[^]
- Also note the difference to how I'm populating the dataGridView from my previous solution (because the list items are now strings)

For completeness, if you already have a DataGridView populated you can convert the columns into Lists to do this manipulation later ... I created a button to remove the duplicates like this

private void removeDups_Click(object sender, EventArgs e)
{

    var redList = dataGridView1.Rows
         .OfType<DataGridViewRow>()
         .Where(x => x.Cells[redColumn].Value != null)
         .Select(x => x.Cells[redColumn].Value.ToString())
         .Distinct()
         .ToList();

    var greenList = (from DataGridViewRow dr in dataGridView1.Rows
                     where dr.Cells[greenColumn].Value != null
                     select dr.Cells[greenColumn].Value.ToString()).Distinct().Except(redList).ToList();

    dataGridView1.Rows.Clear();
    dataGridView1.Rows.Add(Math.Max(greenList.Count(), redList.Count()));

    for (var i = 0; i < dataGridView1.Rows.Count; i++)
    {
        if (i < greenList.Count)
            dataGridView1.Rows[i].Cells[greenColumn].Value = greenList[i];
        if (i < redList.Count)
            dataGridView1.Rows[i].Cells[redColumn].Value = redList[i];
    }

}



注意事项:

- 我使用了两种不同的方法来转换列表中的红色和绿色列 - 使用您感觉更舒适的方法。他们仍然使用 .Distinct() .Except()



正如我在此解决方案的顶部所说的那样 - 当您填充dataGridView(即数据表)的数据源时,最好注意这些事情。我建议替代方案,但我不知道你是如何得到你的数据的。


Things to note:
- I used two different methods to convert the red and green columns to lists - use whichever method you feel more comfortable with. They still both use .Distinct() and .Except()

As I said right at the top of this solution though - these things are best taken care of when you are populating the datasource for the dataGridView (i.e. the datatable). I'd suggest alternatives but I don't know how you are getting your data.


这篇关于如何以特定方式对数据网格视图进行分类和排列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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