【问题标题】:pass datagridview from one form to another c#将datagridview从一个表单传递到另一个c#
【发布时间】:2015-08-16 08:46:59
【问题描述】:

我想将我的 datagridview 从 form1 传递到 form2。我尝试使用构造函数但没有结果,第二种形式的 datagridview 是空的。 有人可以在这里帮助我吗,我已经堆积了几个小时。我不使用 sql,也不需要使用 dataTable。这是我的代码:

我在 datagridview2 的 cellClick 事件上填充 datagridview3。 当我点击 dayagridview2 cellClick_event 我的 datagridview3 充满了这个方法:

 private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        DataD d = new DataD();
        d.Mat(dataGridView1,dataGridView2,dataGridView3);

    }

这是填充 dataGridView3 的方法:

   public void Mat(DataGridView dataGridView1, DataGridView dataGridView2,DataGridView dataGridView3)
    {
        Named n = new Named();

        foreach (DataGridViewCell cell in dataGridView2.SelectedCells)
        {
            IList<String> lista = new List<String>();
            n.Data = string.Empty;
            n.Data2 = string.Empty;
            int indexOfYourColumn = 9;
            int index2 = 0;
            var restaurantList = new List<Named>();
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                n.Data = row.Cells[indexOfYourColumn].Value.ToString();

                if (cell.Value.ToString() == n.Data.ToString())
                {

                    restaurantList.Add(new Nalozi()
                    {
                        Data = row.Cells[indexOfYourColumn].Value.ToString(),
                        Data2 = row.Cells[index2].Value.ToString()
                    });


                }
            }


            dataGridView3.DataSource = restaurantList;
        }


    }

所以现在我只需要在 buttnClick 上以另一种形式显示这个 dataGridView3。

【问题讨论】:

标签: c# winforms datagridview


【解决方案1】:

如果您想将DataGridView 从一个表单传递到另一个表单,您可能需要将DataGridView.DataSource 或DataGridView 从一个表单传递到另一个表单。类似的东西

new SecondForm(dataGridView.DataSource)

您的SecondForm 将接受传递的DataSource 并将其传递给该表单的DataGridView

class SecondForm
{
    public SecondForm(object dataSource)
    {
        InitializeComponents();

        dataGridView.DataSource = dataSource;
    }
}

如果您想传递DataSource 的副本,您可以从DataGridView 的FirstForm 中的现有数据创建一个新的DataTable

private DataTable GetDataTableFromDGV(DataGridView dgv)
{
    var dt = new DataTable();
    foreach (DataGridViewColumn column in dgv.Columns)
    {
        if (column.Visible)
        {
            // You could potentially name the column based on the DGV column name (beware of dupes)
            // or assign a type based on the data type of the data bound to this DGV column.
            dt.Columns.Add();
        }
    }

    object[] cellValues = new object[dgv.Columns.Count];
    foreach (DataGridViewRow row in dgv.Rows)
    {
        for (int i = 0; i < row.Cells.Count; i++)
        {
            cellValues[i] = row.Cells[i].Value;
        }
        dt.Rows.Add(cellValues);
    }

    return dt;
}

并更新对此的第一次调用

new SecondForm(GetDataTableFromDGV(dataGridView))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多