I created a form with two Datagridviews. One of these DataGridViews is filled with data from a Database and it is the source. The second DataGridView should be filled with the selected rows from the source Datagridview after I use a Button.
The first step if fill my DataTable is filled this :
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;
}
After that I fill my source 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();
}
And then I push the "ExportButton" and all Selected Rows are copied to the second DatagRidview. But I don't wont't a copy of the rows in the second DataGridview. I will move the selected rows. So I tried in a remove of the items in a foreach loop:
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);
}
}
But if I tried the deletion in this way. All selected rows be copied but only the last selected row be deleted in the source DatGridView. Whats the best way to delete this selected rows?