在c#中将数据从一个datagridview复制到另一个

时间:2011-05-30 06:16:13

标签: c# datagrid

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;

    }

}  

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

2 个答案:

答案 0 :(得分:5)

检查Copy from datagridview and paste to second datagridview

此外,您可以执行以下操作:

//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);    
}

此致

答案 1 :(得分:1)

在form1中复制此代码

private void Form1_Load(object sender,EventArgs e)
{
    dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;      
    dataGridView1.Columns.Insert(0, new DataGridViewCheckBoxColumn());
    show_chkBox();
    SqlConnection con = new SqlConnection("server=(local);DataBase=RIMS;User 
    d=sa;Password=Rootdb");
    SqlCommand com = new SqlCommand("Select * from Master_City", con);
    SqlDataAdapter da = new SqlDataAdapter(com);
    DataSet ds = new DataSet();
    da.Fill(ds, "city");
    dataGridView1.DataSource = ds;
    dataGridView1.DataMember = "city";
}

private void button1_Click_1(object sender, EventArgs e)
{
    DataGridViewRow dr = dataGridView1.SelectedRows[0];
    dtItems.Columns.Add("city_ID");
    dtItems.Columns.Add("city_Name");
    dtItems.Columns.Add("status");
    dtItems.Columns.Add("date");    
    if (dataGridView1.Rows.Count > 1)
    {
        for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
        {
            if (dataGridView1.Rows[i].Cells[0].Value != null)
            {
                DataRow row;
                row = dtItems.NewRow();  
                row["city_ID"] = dataGridView1.Rows[i].Cells[1].Value.ToString();
                row["city_Name"] = dataGridView1.Rows[i].Cells[2].Value.ToString();    
                row["status"] = dataGridView1.Rows[i].Cells[3].Value.ToString();    
                row["date"] = dataGridView1.Rows[i].Cells[4].Value.ToString();    
                dtItems.Rows.Add(row);    
            }    
        }    
    }    
    Form2 frm = new Form2(dtItems);    
    frm.ShowDialog();    
}    

在form2中复制此代码..

public Form2(DataTable dtIt)    
{    
    dtItems = dtIt;    
    InitializeComponent();    
}    

private void AddEmptyRows()    
{    
    for (int i = 1; i <= 5; i++)    
    {    
        dataGV.Rows.Add();    
    }    
}    

private void Form2_Load(object sender, EventArgs e)
{    
    AddEmptyRows();    
    for (int i = 0; i < dtItems.Rows.Count; i++)    
    {    
        dataGV.Rows[i].Cells[0].Value = dtItems.Rows[i]["city_ID"];    
        dataGV.Rows[i].Cells[1].Value = dtItems.Rows[i]["city_Name"];    
        dataGV.Rows[i].Cells[2].Value = dtItems.Rows[i]["status"];    
        dataGV.Rows[i].Cells[3].Value = dtItems.Rows[i]["date"];    
    }    
    dataGV.Enabled = true;    
}