Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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 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:

C#
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++;
                }

            }
Posted
Updated 26-Mar-16 20:50pm
v3
Comments
So where is the issue?
Armel_Djient 26-Mar-16 14:45pm    
what i have tried does not work
Did you debug and see what is the problem?
Armel_Djient 26-Mar-16 15:01pm    
after debugging i have this message:< Index was out of range. Must be non-negative and less than the size of the collection.>
Which line?

1 solution

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.
C#
//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];
}
 
Share this answer
 
Comments
Armel_Djient 26-Mar-16 18:08pm    
thanks you for your reply. it helped me a lot.

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