我创建了一个包含两个Datagridviews的表单。其中一个DataGridView充满了来自数据库的数据,它是源代码。在使用Button之后,第二个DataGridView应该使用源Datagridview中的选定行填充。将选定的行从DataGridView移动到另一个Visual C#
的第一步,如果填补我的DataTable充满这样的:
public DataTable loadMatImpTable(String query)
{
myConn.Open();
SQLiteCommand cmd = new SQLiteCommand(query, myConn);
SQLiteDataAdapter sda = new SQLiteDataAdapter();
sda.SelectCommand = cmd;
DataTable dt= new DataTable();
sda.Fill(dt);
return dt;
}
在那之后我填写我的源的DataGridView:
DataTable dt = lt.loadMatImpTable(queryMat);
this.matExpDataGridVW.Rows.Clear();
foreach (DataRow item in dt.Rows)
{
int n = matExpDataGridVW.Rows.Add();
matExpDataGridVW.Rows[n].Cells[0].Value = false;
matExpDataGridVW.Rows[n].Cells[1].Value = item["MaterialID"].ToString();
matExpDataGridVW.Rows[n].Cells[2].Value = item["Name"].ToString();
matExpDataGridVW.Rows[n].Cells[3].Value = item["Preis"];
matExpDataGridVW.Rows[n].Cells[4].Value = item["Anzahl"].ToString();
matExpDataGridVW.Rows[n].Cells[5].Value = item["Datum"].ToString();
}
然后我按“ExportButton”,所有选定行复制到第二个DatagRidview。但是我不会在第二个DataGridview中的行的副本。我将移动选定的行。所以我尝试删除foreach循环中的项目:
private void mvImpSelectionBT_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow item in matExpDataGridVW.Rows)
{
if ((bool)item.Cells[0].Value == true)
{
int n = matImpDataGridVW.Rows.Add();
matImpDataGridVW.Rows[n].Cells[0].Value = false;
matImpDataGridVW.Rows[n].Cells[1].Value = item.Cells[1].Value.ToString();
matImpDataGridVW.Rows[n].Cells[2].Value = item.Cells[2].Value.ToString();
matImpDataGridVW.Rows[n].Cells[3].Value = item.Cells[3].Value.ToString();
matImpDataGridVW.Rows[n].Cells[4].Value = item.Cells[4].Value.ToString();
matImpDataGridVW.Rows[n].Cells[5].Value = item.Cells[5].Value.ToString();
}
}
#Delete all Selected rows
foreach (DataGridViewRow item in matExpDataGridVW.SelectedRows)
{
matExpDataGridVW.Rows.Remove(item);
}
}
但是,如果我尝试删除这种方式。所有选中的行都被复制,但只有最后一个选定的行才会在源DatGridView中被删除。什么是删除这些选定行的最佳方法?