1

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

我怎样才能做到这一点?

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

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

4 回答 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());
    }
}
于 2013-08-14T11:05:10.973 回答
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();
于 2015-06-12T16:01:07.153 回答
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 
于 2017-11-02T07:04:27.183 回答
0

为什么要复制而不是您可以传递参考。

首先为源创建数据表

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

点击按钮

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

Form2 具有重载的构造函数。

于 2017-11-02T07:10:04.833 回答