使用TableAdapter重新加载数据

时间:2012-05-01 19:32:11

标签: c# sql winforms dataset dataadapter

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。

使用Designer将数据绑定到Datagrid

2 个答案:

答案 0 :(得分:4)

首先,我将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告诉您的主窗体仅在实际发生更新时触发数据刷新。

答案 1 :(得分:1)

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

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

并且从偶数处理程序调用此函数后,当某人以其他形式更改某些内容时会引发该函数。

Events tutorial