简体   繁体   中英

How To Remove Automatic Datagridview in c#

i have datagridview like below :

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

i try to automatic execute rows in datagridview the use thread.sleep. while i execute button StartAuthorization, i want execute the data Rows one by one and remove rows if rows after executed; 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 . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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