c# - 将 datagridview 从一种形式传递到另一种形式 C#

标签 c# winforms datagridview

我想将我的 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。

最佳答案

如果你想通过DataGridView从一种形式到另一种形式,您可能必须通过 DataGridView.DataSourceDataGridView从一种形式到另一种形式。类似的东西

new SecondForm(dataGridView.DataSource)

和你的SecondForm将接受通过 DataSource并将其传递给 DataGridView那种形式的

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

        dataGridView.DataSource = dataSource;
    }
}

如果你想传递DataSource的副本你可以创建一个新的 DataTable来自内部的现有数据 DataGridViewFirstForm

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))

关于c# - 将 datagridview 从一种形式传递到另一种形式 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32033325/

相关文章:

c# - Socket 编程 c#/客户端-服务器通信

c# - 您可以通过 App.config 文件更改 C# Winforms 按钮的名称吗?

C# 窗体自动调整窗口大小

C# Gridview 第一行数据不返回

c# - 以编程方式从 DataTable 创建 DataGridview

c# - Marshal.GetDelegateForFunctionPointer 失败

c# - 除了 xamarin 中的 Assets 之外,还可以使用代码加载关卡

c# - Lambda 属性名称和数组索引

c# - 在windows窗体中显示透明gif文件

excel - 如何启用从 Excel 复制粘贴到自定义 DataGridView?