使用 TableAdapter 重新加载数据


private void UserList_Load(object sender, EventArgs e)
{
    // TODO: This line of code loads data into the 'workOrdersDataSet.users' table. You can move, or remove it, as needed.
    this.usersTableAdapter.Fill(this.workOrdersDataSet.users);
}

如果以其他形式进行了更改,如何重新加载数据?(最好是自动使用的,不使用刷新按钮)?

我正在使用WinForms,后端是Access 2007。

使用设计器将数据绑定到数据网格

首先,我将Fill移动到一个单独的函数:

public void LoadData()
{
   this.usersTableAdapter.Fill(this.workOrdersDataSet.users);
}

然后,当您执行加载事件时,您将调用该函数:

private void UserList_Load(object sender, EventArgs e)
{
   LoadData();
}

如果您有另一个对数据执行更改的窗体,则可以在另一个事件中调用此函数,类似于此事件。我在代码中使用DialogResult

private void OpenOtherForm()
{
    DialogResult openForm = new OtherForm().ShowDialog();
    if(openForm == DialogResult.OK)
        LoadData();
}
更新

过程完成后,在另一个窗体的代码中,包含一行代码以指示主窗体进行更新:

private void PerformUpdate()
{
    try
    {
        // your update code goes here
        DialogResult = DialogResult.OK; // this is the line that tells your other form to refresh
    }
    catch (Exception ex)
    {
        DialogResult = DialogResult.Abort;
    }
}

然后,使用DialogResult,告知主窗体仅在实际发生更新时触发数据刷新。

您可以将此行添加到另一个函数中,例如

public void MoveDataToUI()
{
   this.usersTableAdapter.Fill(this.workOrdersDataSet.users);
}

之后从 even 处理程序调用此函数,当有人以另一种形式更改某些内容时会引发该处理程序。

事件教程

最新更新