BindingSource 在 TableAdapter.Fill 之后不使用 MoveNext() 移动到下一条记录

BindingSource not moves to next record with MoveNext() after TableAdapter.Fill

我在下面的按钮上点击了这个事件:

private void button4_Click(object sender, EventArgs e)
{
    string connectionString2 = "Data Source=LPMSW09000012JD\SQLEXPRESS;Initial Catalog=Pharmacies;Integrated Security=True";
    string query2 = "UPDATE Liguanea_Lane2 SET Progress= '1' where code = '" + comboBox2.Text + "'; ";
    using (SqlConnection connection = new SqlConnection(connectionString2))
    {
        SqlCommand command = new SqlCommand(query2, connection);
        command.Connection.Open();
        command.ExecuteNonQuery();
    }
    comboBox2.ResetText();
    textBox1.Clear();
    comboBox2.SelectedIndex = comboBox2.SelectedIndex + 1;
    this.liguanea_Lane2TableAdapter.Fill(this.pharmaciesDataSet3.Liguanea_Lane2);
    liguaneaLane2BindingSource.MoveNext();
}

问题是这个特定的代码块:

this.liguanea_Lane2TableAdapter.Fill(this.pharmaciesDataSet3.Liguanea_Lane2);
liguaneaLane2BindingSource.MoveNext();

它所做的基本上是刷新数据网格中的数据源并移动到 table 中的下一个项目。 Eg.After 按钮被点击它更新 "progress" 列值到“1”然后刷新数据集然后调用 .MoveNext 将光标移动到 table 中的下一个项目.它有效但只执行一次然后停止移动。数据集刷新正常,但 .MoveNext 是问题所在。我尝试将它移到数据集上方,但它没有执行问题。我做错了什么?

当您调用 TableAdapter.Fill(Table) 时,它会导致绑定到 TableBindingSource 移动到第一条记录。所以在填充 table 之后调用 bindingSource.MoveNext() 总是移动到第二条记录。

如果出于任何原因您想在使用 table 适配器填充 table 后移动到下一条记录,请在填充前存储当前位置并在填充后将位置设置为下一条记录:

int current = 0;
private void Button1_Click(object sender, EventArgs e)
{
    current = bindingSource1.Position;
    tableAdapter1.Fill(dataSet1.Table1);
    bindingSource1.Position = Math.Min(current + 1, bindingSource1.Count - 1);
}