如何在特定列中的特定单元格中添加数据 [英] How to add data in a specific cell from specific column

查看:80
本文介绍了如何在特定列中的特定单元格中添加数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我有c#winform

i定义了一个数据表,其中有数据。

现在我想获取所有社会名称(数据表列)和商店他们在datagridview中如下

hi all, i have c# winform
i defined a datatable in which there is data.
now i would to fetch all society name (column of datatable) and store them in datagridview as follow

-----------------------
GREEN       | RED     |          //headers
-----------------------
A           |   B     |         
-----------------------
C           |   D      |
-----------------------
G           |   K      |
-----------------------



A,B,C,D, G,K是社团。我想要做的是:从数据表中列[status] =green的所有社会都转到datagridview的[GREEN]列

和所有社团列[status] =red列转到datagridview的[RED]列。



我尝试过:




A,B,C,D,G,K are societies.WHAT I WANT TO DO is: all the societies where the column [status]="green" from datatable go to the [GREEN ]column of datagridview
and all the societies where the column [status]="red" go to the [RED ]column of datagridview .

What I have tried:

datatable  dchild=new datatable();
this.dataGridView1.Rows.Add();
int cell_green=0,cell_red=0;
  foreach (DataRow item in dchild.Rows)
            {
                var society_name=item["Society_Id"].ToString();

                if (item["Flag_created"].ToString() == "green")
                {
                   this.dataGridView1.Rows[cell_green].Cells[0].Value = society_name;
                    cell_green++;
                }
                else
                {
                    this.dataGridView1.Rows[cell_red].Cells[1].Value = society_name;
                    cell_red++;
                }

            }

推荐答案

如果我这样做,我会离开考虑DataTable或DataGridView并考虑我试图呈现的信息作为两个单独的项目列表 - 绿色项目和红色项目。



如果我只是通过数据表工作,没有简单的方法来告诉何时增加行号,或者我必须继续回到相应列中的下一个空单元格。



但是,如果我已经分离出两个项目列表,我知道我需要多少行(每个列表中的项目数最多),我总是知道列表中的行踪我需要看...我只是每次弹出下一个项目......检查后确保列表中还剩下任何东西!



Eg
If I was doing something like this I would move away from thinking about the DataTable or the DataGridView and consider the information I'm trying to present as two separate lists of items - the green items and the red items.

If I just work my way through the datatable there is no easy way of telling when to increment the row number, or I have to keep going back to the next empty cell in the appropriate column.

However, if I have the two lists of items already separated out, I know how many rows I'm going to need (maximum of the count of items from each list), and I always know whereabouts in the list I need to look ... I just "pop" the next item each time...after checking to make sure there is anything left in the list!

E.g.
//Get the "green" items
List<DataRow> greenItems = dt.Select("Flag_created ='Green'").ToList();
int g = greenItems.Count();
//get the "red" items
List<DataRow> redItems = dt.Select("Flag_created ='Red'").ToList();
int r = redItems.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].ItemArray[0];
    if (i < redItems.Count)
        dataGridView1.Rows[i].Cells[redColumn].Value = redItems[i].ItemArray[0];
}


这篇关于如何在特定列中的特定单元格中添加数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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