如何在c#中删除自动数据视图

本文关键字:数据 视图 删除 | 更新日期: 2023-09-27 18:10:25

我有如下的datagridview:

| FIleName      | Amount     | Description |
--------------------------------------------
| Test001       | 1000       |             |
| Test002       | 1000       |             |
| Test003       | 1000       |             |
如果我尝试自动执行datagridview中的行,使用thread.sleep。而我执行按钮StartAuthorization,我想执行数据行一个接一个,如果行执行后删除行;我试过这个代码:
private void button7_Click_1(object sender, EventArgs e)
{
 Thread.Sleep(10000);
 while (dataGridView1.Rows.Count > 0)
 {
  RunAtomationRun(); // method to execute rows in datagridview
  int selectedrowindex = dataGridView1.SelectedCells[0].RowIndex;
  DataGridViewRow selectedRow = dataGridView1.Rows[selectedrowindex];
  DataGridView1.Rows.RemoveAt(this.dataGridView1.SelectedCells[0].RowIndex);
 }
}

但不工作?任何想法?

如何在c#中删除自动数据视图

在使用selectedrowindex之前,您应该检查是否有实际选择的行。但是函数如何选择要处理的行呢?

你应该把该行传递给函数,并在函数成功时删除它:

private void button7_Click_1(object sender, EventArgs e)
{
 Thread.Sleep(10000);
 while (dataGridView1.Rows.Count > 0)
 {
  DataGridViewRow currentRow = dataGridView1.Rows[0];
  RunAtomationRun(currentRow); // method to execute rows in datagridview
  DataGridView1.Rows.Remove(currentRow);
 }
}

或者让函数在成功时删除行,我想它已经知道它正在处理哪一行:

private void RunAtomationRun()
{
 // Find row to work on
 DataGridViewRow currentRow = ...
 // Work on row...
 // Delete Row on success
 DataGridView1.Rows.Remove(currentRow);
}