DataTable GetSelectedRows(DataGridView dgv)
{
var dt = new DataTable();
foreach (DataGridViewColumn column in dgv.Columns)
{
if (column.Visible)
{
// You could potentially name the column based on the DGV column name (beware of dupes)
// or assign a type based on the data type of the data bound to this DGV column.
dt.Columns.Add();
}
}
object[] cellValues = new object[dgv.Columns.Count];
foreach (DataGridViewRow row in dgv.Rows)
{
if (!row.Selected) continue; // Add only Selected Rows
for (int i = 0; i < row.Cells.Count; i++)
cellValues[i] = row.Cells[i].Value;
dt.Rows.Add(cellValues);
}
return dt;
}
1 回答
您可以创建一个新的
DataTable
并将所有选定的行插入DataTable
您可以使用此方法将所有SelectedRows传递给新的DataGridView