Reload data using TableAdapter

梦想的初衷 提交于 2019-12-05 13:11:32

First, I would move the Fill to a separate function:

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

Then when you do your Load Event, you will call the function:

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

If you have another form that performs changes on the data, you can call this function in another event, similar to this. I use DialogResult in my code:

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

In your code for the other Form after your update process is complete, include a line of code to tell your main form to update:

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;
    }
}

using the DialogResult then, tells your main form to trigger the Refresh of the Data only when an Update actually takes place.

You can add this line into another function, say

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

and after call this function from the even handler, which is raised when someone changes something in another form.

Events tutorial

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!