首页 文章

如何将form1中的数据网格视图的内容复制到c#中form2中的另一个数据网格视图

提问于
浏览
1

我有2个胜利表格 . 在表单1中,我有一个 dataGridView ,我将收集所有数据 .
现在我想要做的是,当我按下表单1中的一个按钮时,我想显示表单2,并且需要将form1数据网格视图中的所有行复制到表单2中的数据网格视图 .

我怎样才能做到这一点?

如果这不可能,是否可以将form1中的数据网格视图作为浮动控件(可以在UI上自由移动)?

我可以使用下面的代码以相同的形式从一个 dataGridView 复制到另一个 dataGridView .

private DataGridView CopyDataGridView(DataGridView dgv_org)
    {
        try
        {
            if (dataGridView2.Columns.Count == 0)
            {
                foreach (DataGridViewColumn dgvc in dgv_org.Columns)
                {
                    dataGridView2.Columns.Add(dgvc.Clone() as DataGridViewColumn);
                }
            }
            DataGridViewRow row = new DataGridViewRow();
            int k = 2;
            if (first == false)
            {
                k=0;
                first = true;
            }
                for (int i = k; i < dgv_org.Rows.Count; i++)
                {
                    row = (DataGridViewRow)dgv_org.Rows[i].Clone();
                    int intColIndex = 0;
                    foreach (DataGridViewCell cell in dgv_org.Rows[i].Cells)
                    {
                        row.Cells[intColIndex].Value = cell.Value;
                        intColIndex++;
                    }
                    dataGridView2.Rows.Add(row);
                }
                dataGridView2.AllowUserToAddRows = false;
                dataGridView2.Refresh();                
        }
        catch (Exception ex)
        {
            MessageBox.Show("Copy DataGridViw Error");
        }
        return dataGridView2;
    }

但是不能复制到form2 .

4 回答

  • 0
    private void CopyAll(DataGridView from, DataGridView to)
    {
        if (to.Columns.Count == 0)
        {
            foreach (DataGridViewColumn dgvc in from.Columns)
            {
                to.Columns.Add(dgvc.Name, dgvc.HeaderText);
            }
        }
    
        to.Rows.Clear();
    
        foreach(DataGridViewRow dgvr in from.Rows)
        {
            List<string> cells = new List<string>();
    
            foreach(DataGridViewCell dgvc in dgvr.Cells)
            {
                cells.Add(dgvc.Value.ToString());
            }
    
            to.Rows.Add(cells.ToArray());
        }
    }
    
  • 0

    可能你现在不需要这个,但希望将来能帮助别人 .

    // Retrieve data from database in form1
     conn.Open();
     SqlDataAdapter sda = new SqlDataAdapter("Select Image,Name from Input where ID = '" + txt_LID.Text + "'",conn);
     DataTable dt = new DataTable();
     sda.Fill(dt);
     dGV_form1.DataSource = dt; 
    
     // Button Code on form1          
     Form2 lcp = new Form2();
     lcp.dGV_form2.DataSource = dt;
     lcp.Show();
     this.Hide();
    
  • 0
    frm.dataGridView2.Rows.Clear();
                    if (dataGridView1.Rows.Count > 0)
                    {
                        for (int i = 0; i < dataGridView1.Rows.Count; i++)
                        {
                            if (dataGridView1.Rows[i].Cells[0].Value != null)
                            {
                                frm.dataGridView2.Rows.Add();
                                frm.dataGridView2.Rows[i].Cells[0].Value = dataGridView1.Rows[i].Cells[0].Value.ToString();
                                frm.dataGridView2.Rows[i].Cells[1].Value = dataGridView1.Rows[i].Cells[1].Value.ToString();
                                frm.dataGridView2.Rows[i].Cells[2].Value = dataGridView1.Rows[i].Cells[2].Value.ToString();
                                frm.dataGridView2.Rows[i].Cells[3].Value = dataGridView1.Rows[i].Cells[3].Value.ToString();
                                frm.dataGridView2.Rows[i].Cells[4].Value = dataGridView1.Rows[i].Cells[4].Value.ToString();
                                frm.dataGridView2.Rows[i].Cells[5].Value = dataGridView1.Rows[i].Cells[5].Value.ToString();
                            }
                        }
                    }   this 100% works i have used it
    
  • 0

    为什么要复制,你可以传递参考 .

    首先为源创建数据表

    DataTable dt = new Datatable();
    //Populate datatable
    gridView.DataSource =dt;
    

    单击按钮

    Form2 f2 = new Form2(dt);
    f2.Show();
    

    Form2重载了构造函数 .

相关问题