我的表格:
我想删除数据集中的选定行。按钮"删除"有这个处理程序:
int indexForDeleting = 0;
bool reason = true;
foreach (DataGridViewRow item in this.dataGridView1.SelectedRows)
{
this.dataGridView1.Rows.RemoveAt(item.Index);
if (reason)
{
indexForDeleting = item.Index;
reason = false;
}
}
dataSet11.flight.Rows.Remove(dataSet11.flight.Rows[Math.Abs(indexForDeleting)]);
dataSet11.flight.AcceptChanges();
仅在DataGridView中删除所选行,但我希望在DataGridView和DataSet中删除行。
答案 0 :(得分:0)
如果该网格绑定到DataTable,则删除该表的DataRow,而不是网格行。像这样:
int rowIdx = dataGridView1.CurrentCell.RowIndex;
DataRowView drv = (DataRowView)dataGridView1.Rows[rowIdx].DataBoundItem;
DataRow dr = drv.Row;
dr.Delete();
这将删除行并更新网格。