Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have a datatable updated from sql server, i want to fill specific columns from datatable into specific columns into datagridview
here is my code :
C#
foreach (DataRow row in usersInincident.Rows)
                        {
                            dataGridView5.AllowUserToAddRows = false;
                            dataGridView5.Rows.Add();

                            var v  = row["PuserNickname"].ToString();
                            var v1 = row["puserserialno"].ToString();
                            var v2 = row["puserfname"].ToString();
                            var v3 = row["puserlname"].ToString();
                            var v4 = row["gradename"].ToString();
                            var v5 = row["tokenname"].ToString();
                            var v6 = row["policeuserrole"].ToString();



                            foreach (DataGridViewRow gridRow in dataGridView5.Rows)
                            {
                                DataGridViewCell cell = gridRow.Cells[1] as DataGridViewCell;
                                DataGridViewCell cell1 = gridRow.Cells[2] as DataGridViewCell;
                                DataGridViewCell cell2 = gridRow.Cells[3] as DataGridViewCell;
                                DataGridViewCell cell3 = gridRow.Cells[4] as DataGridViewCell;
                                DataGridViewCell cell4 = gridRow.Cells[5] as DataGridViewCell;
                                DataGridViewCell cell5 = gridRow.Cells[6] as DataGridViewCell;
                                DataGridViewCell cell6 = gridRow.Cells[7] as DataGridViewCell;

                                cell.Value = v;
                                cell1.Value = v1;
                                cell2.Value = v2;
                                cell3.Value = v3;
                                cell4.Value = v4;
                                cell5.Value = v5;
                                cell6.Value = v6;

                            }
                        }


What I have tried:

it is giving me the same row when filling the dgv
Posted
Updated 9-Sep-16 7:29am
v2
Comments
[no name] 9-Sep-16 12:14pm    
What does your debugger tell you when you step through this?
ramy nemer 9-Sep-16 12:26pm    
it passes through the first foreach only one time once it enters the second foreach it loops through it until it finishes (values are not changing from the 1st foreach)
[no name] 9-Sep-16 12:43pm    
Well then that is your answer. You are just filling the second grid with one row from the first grid. If that is not what you want to do then don't do that.
ramy nemer 9-Sep-16 12:54pm    
the datatable have three rows,what its doing is its filling the dgv in three same rows,how should i fill them in dgv in specifying its columns ,without using databind
[no name] 9-Sep-16 13:13pm    
Yes, that is exactly what you told it to do. Computers are stupid, they only do what you tell them to. Start by not filling the second grid. Only put the data into the second grid that you want to put there.

1 solution

You need to learn to use the debugger! If you step through your code, the problem will be obvious.

Look again at what your code is doing:
for each row in the source:
    add a new row to the destination
    extract the values from the source row
    
    for each row in the destination, including all previously added rows
        set the columns to the values extracted from the source
    {end of inner loop}
{end of outer loop}


If your source has two rows, you add the first row to the destination and update it with the values from the first row in the source. Then, you add a second row to the destination, and update both destination rows with the values from the second row in the source.

The solution is also obvious: don't update every row in the destination; only update the row you've just added.
C#
foreach (DataRow row in usersInincident.Rows)
{
    int indexOfNewRow = dataGridView5.Rows.Add();
    DataGridRow gridRow = dataGridView5.Rows[indexOfNewRow];
    
    // Now copy the values from row to gridRow...
}
 
Share this answer
 
Comments
Maciej Los 9-Sep-16 13:31pm    
Great explanation!
I wouldn't be able to explain it better.

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