如何在DataGridView空间上删除表行?

来源:百度知道 编辑:UC知道 时间:2024/07/12 14:23:38
如图所示,当选中DataGridView控件中某一行时,按“添加到报废列表”按钮然后会执行删除相应的行,这个怎样在代码上实现。
我现在就是不懂怎样将按钮与选中的行关联起来,在某处看到的提示:
private void button2_Click(object sender,System.EventArgs e)
{
dataGridView1.Rows.Remove(dataGridView1.SelectedRows)
} 不行,更本就不能关联起来,现实的错误是“Error 1 The best overloaded method match for 'System.Windows.Forms.DataGridViewRowCollection.Remove(System.Windows.Forms.DataGridViewRow)' has some invalid arguments C:\Documents and Settings\Tony\Desktop\stockroom\stockroom\DiscardStorage.cs 29 13 stockroom”
“Error 2 Argument '1': cannot convert from 'System.Windows.Forms.DataGridViewSelectedRowCollection' to 'System.Windows.Forms.DataGridViewRow' C:\Documents and Settings\Tony\Desktop\stockroom\stockroom\DiscardStorage.cs 29 44 stockroom”
请好心的高手帮帮忙!~

dataGridView1.SelectedRows是一个集合 不是指一行
private void button2_Click(object sender, EventArgs e)
{
int i=1;
dataGridView1.CurrentRow.Selected = true;
DataGridViewRow r = new DataGridViewRow();
for (i = 0; i < dataGridView1.SelectedCells.Count; i++)
{
dataGridView1.Rows.Remove(dataGridView1.SelectedRows[i]);
}
}

这样能删除dataGridView的一行 不过不能删数据库里的数据

DataGridView1.Rows.Remove(DataGridView1.CurrentRow)'删除当前光标所在行
DataGridView1.Rows.Remove(DataGridView1.Rows(DataGridView1.Rows.Count - 1))'删除最后一行
DataGridView1.Rows.Clear()'删除所有行