将数据从一个datagridview复制到另一个c# [英] Copy data from one datagridview to another in c#

查看:377
本文介绍了将数据从一个datagridview复制到另一个c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(int i = 0; i< form2.dataGridView1.Rows.Count; i ++)
{

if(form2.dataGridView2 .Rows [i] .Cells [0] .Value!= null&&&b $ b(bool)form2.dataGridView2.Rows [i] .Cells [0] .Value == true)
{
form2.dataGridView2.Rows.Add();
for(int j = 1; j< form2.dataGridView1.Columns.Count; j ++)
form2.dataGridView2.Rows [i] .Cells [j] .Value =
form2。 dataGridView1.Rows [i] .Cells [j] .Value;

}

}

上述代码没有任何结果。请告诉我如何将一个datagridview的数据复制到另一个?

解决方案

检查从datagridview复制并粘贴到第二个datagridview



另外,你可以这样做:

  //将datagridview绑定到linq 
var gd1 =
从datagridview.Rows.Cast< DataGridViewRow>()
选择新的{Column1 = a.Cells [Column1]。Value.ToString()})。tolist();

//循环dg1并将其保存到datagridview2
foreach(dg1中的var b)
{
datagridview2.Rows.Add(b.Column1);
}

关于


for (int i = 0; i < form2.dataGridView1.Rows.Count; i++)
{                  

    if ( form2.dataGridView2.Rows[i].Cells[0].Value != null && 
         (bool) form2.dataGridView2.Rows[i].Cells[0].Value == true )
    {
        form2.dataGridView2.Rows.Add();
        for (int j = 1; j < form2.dataGridView1.Columns.Count; j++)
            form2.dataGridView2.Rows[i].Cells[j].Value = 
                form2.dataGridView1.Rows[i].Cells[j].Value;

    }

}  

the above code is not giving any result. please tell me how to copy the data of one datagridview to another?

解决方案

Check Copy from datagridview and paste to second datagridview.

Also, you can do as the following:

//Bind datagridview to linq 
var gd1 = 
    ( from a in datagridview.Rows.Cast<DataGridViewRow>()
      select new {Column1 = a.Cells["Column1"].Value.ToString() }).tolist();

//loop dg1 and save it to datagridview2
foreach(var b in dg1)
{
    datagridview2.Rows.Add(b.Column1);    
}

Regards

这篇关于将数据从一个datagridview复制到另一个c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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