简体   繁体   English

如何在C#中删除自动Datagridview

[英]How To Remove Automatic Datagridview in c#

i have datagridview like below : 我有如下的datagridview:

| FIleName      | Amount     | Description |
--------------------------------------------
| Test001       | 1000       |             |
| Test002       | 1000       |             |
| Test003       | 1000       |             |

i try to automatic execute rows in datagridview the use thread.sleep. 我尝试在datagridview中自动执行使用thread.sleep的行。 while i execute button StartAuthorization, i want execute the data Rows one by one and remove rows if rows after executed; 当我执行按钮StartAuthorization时,我想一一执行数据行,如果执行后删除行,则删除行; i tried this code : 我尝试了这段代码:

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);
 }
}

but not works ? 但是行不通? any idea? 任何想法?

You should check if there are actually rows selected before using selectedrowindex . 在使用selectedrowindex之前,您应该检查是否实际选择了行。 But how does your function select the row to work on? 但是您的函数如何选择要处理的行?

You should really pass the row to the function and remove it when the function succeeds: 您应该真正将行传递给函数,并在函数成功后将其删除:

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);
 }
}

Or let the function remove the row on success, I suppose it already knows wich one it is working on: 或者让函数成功删除行,我想它已经知道它正在处理的工作:

private void RunAtomationRun()
{
 // Find row to work on
 DataGridViewRow currentRow = ...

 // Work on row...

 // Delete Row on success
 DataGridView1.Rows.Remove(currentRow);
}

Both variants should fix your problem. 两种变体都可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM