DataGridView to DataTable
Publicado por William (1 intervención) el 15/12/2020 19:44:11
Hola, quisiera pasar los datos de DataGridView a un DataTable, buscando en internet me encontré este código, pero por alguna razón que desconozco no me funciona, devuelve un DataTable null. Alguien me podría ayudar. Saludos
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
private DataTable GetDataTableFromDGV(DataGridView dgv)
{
var dt = new DataTable();
foreach (DataGridViewColumn column in dgv.Columns)
{
if (column.Visible)
{
dt.Columns.Add();
}
}
DataRow workRow;
foreach (DataGridViewRow row in dgv.Rows)
{
workRow = dt.NewRow();
for (int i = 0; i < row.Cells.Count; i++)
{
workRow[i] = row.Cells[i].Value;
}
dt.Rows.Add(workRow);
}
return dt;
}
Valora esta pregunta


0