如何将值从datatable插入到特定的dgv列 [英] How to insert values from datatable to specific dgv columns

查看:93
本文介绍了如何将值从datatable插入到特定的dgv列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有从sql server更新的数据表,我想从datatable填充特定列到datagridview

这里是我的代码:

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 :

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;

                            }
                        }





我的尝试:



在填写dgv



What I have tried:

it is giving me the same row when filling the dgv

推荐答案

时给我相同的行你需要学会使用调试器!如果你单步执行代码,问题就会很明显。



再看看你的代码在做什么:

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.

foreach (DataRow row in usersInincident.Rows)
{
    int indexOfNewRow = dataGridView5.Rows.Add();
    DataGridRow gridRow = dataGridView5.Rows[indexOfNewRow];
    
    // Now copy the values from row to gridRow...
}


这篇关于如何将值从datatable插入到特定的dgv列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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