使用VB / C#.NET中的DataGridView数据填充数据表

时间:2022-10-01 21:01:32

How can I fill Datatable with DataGridView data (VB/C# .NET)?

如何使用DataGridView数据(VB / C#.NET)填充Datatable?

1 个解决方案

#1


5  

Assuming that it is a Winform

假设它是一个Winform

below code can be of help

下面的代码可以提供帮助

        //dgv is the name of your data grid view.

        DataTable dt = new DataTable();
        DataColumn[] dcs = new DataColumn[]{};

        foreach (DataGridViewColumn c in dgv.Columns)
        {
            DataColumn dc = new DataColumn();
            dc.ColumnName = c.Name;
            dc.DataType = c.ValueType;
            dt.Columns.Add(dc);

        }

        foreach (DataGridViewRow r in dgv.Rows)
        {
            DataRow drow = dt.NewRow();

            foreach (DataGridViewCell cell in r.Cells)
            {
                drow[cell.OwningColumn.Name] = cell.Value;
            }

            dt.Rows.Add(drow);
        }

#1


5  

Assuming that it is a Winform

假设它是一个Winform

below code can be of help

下面的代码可以提供帮助

        //dgv is the name of your data grid view.

        DataTable dt = new DataTable();
        DataColumn[] dcs = new DataColumn[]{};

        foreach (DataGridViewColumn c in dgv.Columns)
        {
            DataColumn dc = new DataColumn();
            dc.ColumnName = c.Name;
            dc.DataType = c.ValueType;
            dt.Columns.Add(dc);

        }

        foreach (DataGridViewRow r in dgv.Rows)
        {
            DataRow drow = dt.NewRow();

            foreach (DataGridViewCell cell in r.Cells)
            {
                drow[cell.OwningColumn.Name] = cell.Value;
            }

            dt.Rows.Add(drow);
        }