2
我的问题是将选定的行从数据网格视图转移到另一个数据网格视图。我不知道如果使用主键并从数据库获取值到另一个数据库或将选定行的数据从datagridview1发送到datagridview2,我不知道该使用什么。将选定的行从数据网格视图转移到另一个数据网格视图
我的问题是将选定的行从数据网格视图转移到另一个数据网格视图。我不知道如果使用主键并从数据库获取值到另一个数据库或将选定行的数据从datagridview1发送到datagridview2,我不知道该使用什么。将选定的行从数据网格视图转移到另一个数据网格视图
您可以创建一个新的DataTable
并插入所有选定的行到DataTable
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;
}
您可以使用此
dataGridView2.DataSource = GetSelectedRows(dataGridView1)
通过所有SelectedRows新的DataGridView