首页 > 解决方案 > 将 DataGridView 转换为数据表

问题描述

我必须将datagridview转换为datatable,才能使用下面的代码,但是我有一些问题,datagridview的第一行和最后一行没有插入到datatable和hours字段中,包含这种类型的值08:54 ,当它插入数据表时,值变为08:00,我该如何解决这个问题?

C#代码:

DataTable dtemp = new DataTable();
dtemp.Clear();
dtemp.Columns.Add("Giorno");
dtemp.Columns.Add("DataINS");
dtemp.Columns.Add("Ore");
for (int row = 0; row < dataGridViewPrincipale.Rows.Count; row++)
{
   DataRow riga = dtemp.NewRow();
   riga["Giorno"] = dataGridViewPrincipale.Rows[row].Cells[0].Value.ToString();
   riga["DataINS"] = dataGridViewPrincipale.Rows[row].Cells[1].Value.ToString();
   riga["Ore"] = dataGridViewPrincipale.Rows[row].Cells[2].Value.ToString();
   dtemp.Rows.Add(riga);
}

标签: c#datagridview

解决方案


你能试试这个吗?

我将 datagridView 的数据源转换为 DataTable 并放入数据表中。

DataTable dtemp = new DataTable();
dtemp = dataGridViewPrincipale.DataSource as DataTable;

推荐阅读