![](/img/trans.png)
[英]How to do Automatic synchronize between database access and DatagridView in c#
[英]How To Remove Automatic Datagridview in c#
我有如下的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);
}
}
但是行不通? 任何想法?
在使用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);
}
两种变体都可以解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.