here is the code:
public DataTable DataGridView2DataTable(DataGridView dgv, int minRow = 0)
{
DataTable dt = new DataTable();
// Header columns
foreach (DataGridViewColumn column in dgv.Columns)
{
DataColumn dc = new DataColumn(column.Name.ToString());
dt.Columns.Add(dc);
}
// Data cells
for (int i = 0; i < dgv.Rows.Count; i++)
{
DataGridViewRow row = dgv.Rows[i];
DataRow dr = dt.NewRow();
for (int j = 0; j < dgv.Columns.Count; j++)
{
dr[j] = (row.Cells[j].Value == null) ? "" : row.Cells[j].Value.ToString();
}
dt.Rows.Add(dr);
}
// Related to the bug arround min size when using ExcelLibrary for export
for (int i = dgv.Rows.Count; i < minRow; i++)
{
DataRow dr = dt.NewRow();
for (int j = 0; j < dt.Columns.Count; j++)
{
dr[j] = " ";
}
dt.Rows.Add(dr);
}
return dt;
}
public DataTable DataGridView2DataTable(DataGridView dgv, int minRow = 0)
{
DataTable dt = new DataTable();
// Header columns
foreach (DataGridViewColumn column in dgv.Columns)
{
DataColumn dc = new DataColumn(column.Name.ToString());
dt.Columns.Add(dc);
}
// Data cells
for (int i = 0; i < dgv.Rows.Count; i++)
{
DataGridViewRow row = dgv.Rows[i];
DataRow dr = dt.NewRow();
for (int j = 0; j < dgv.Columns.Count; j++)
{
dr[j] = (row.Cells[j].Value == null) ? "" : row.Cells[j].Value.ToString();
}
dt.Rows.Add(dr);
}
// Related to the bug arround min size when using ExcelLibrary for export
for (int i = dgv.Rows.Count; i < minRow; i++)
{
DataRow dr = dt.NewRow();
for (int j = 0; j < dt.Columns.Count; j++)
{
dr[j] = " ";
}
dt.Rows.Add(dr);
}
return dt;
}
Comments
Post a Comment