如何使从DataGridView DataTable中没有任何数据源? [英] How to make a DataTable from DataGridView without any Datasource?

查看:171
本文介绍了如何使从DataGridView DataTable中没有任何数据源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从网格值的DataGridView中得到的DataTable。

I want to get a DataTable from DataGridView of the Grid values.

在换句话说数据表一样的DataGridView值

In other words DataTable same as DataGridView Values

推荐答案

可能是一个更好的方式来做到这一点,但是,否则它会通过DGV相当琐碎,只是环和手动创建数据表。

Might be a nicer way to do it but otherwise it would be fairly trivial to just loop through the DGV and create the DataTable manually.

这样的事情可能工作:

DataTable dt = new DataTable();
foreach(DataGridViewColumn col in dgv.Columns)
{
   dt.Columns.Add(col.HeaderText);    
}

foreach(DataGridViewRow row in dgv.Rows)
{
    DataRow dRow = dt.NewRow();
    foreach(DataGridViewCell cell in row.Cells)
    {
        dRow[cell.ColumnIndex] = cell.Value;
    }
    dt.Rows.Add(dRow);
}

这篇关于如何使从DataGridView DataTable中没有任何数据源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆