刷新winform上显示的绑定数据需要什么? [英] What is required to refresh bound data displayed on winform?

查看:36
本文介绍了刷新winform上显示的绑定数据需要什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:

  • 通过按钮显示的子表单.
  • 创建委托以在此子项关闭时运行某些代码.
  • 子表单用于编辑底层数据
  • 当子窗体关闭时,最新版本的数据应该显示在父窗体上的任何绑定控件上.

问题 -

这是相关的代码尝试:

public partial class uxRevisionHelperForm : Form
{

    public SqlCeConnection conn = new SqlCeConnection(ConfigurationManager.ConnectionStrings["WindFormAppRevisionHelper.Properties.Settings.DefinitionsDBConnectionString"].ConnectionString);
    BindingSource definitionsBindingSource = new BindingSource();

    public uxRevisionHelperForm()
    {
        InitializeComponent();
        uxDescriptionTextBox.AutoSize = true;
        refreshBindingSource();
        assignControlsToSource();
    }

      //>>>>>>>>ALL OF THE FOLLOWING METHOD IS CALLED BY THE DELEGATE WHEN THE CHILD IS CLOSED
    public void refreshBindingSource()
    {            

        SqlCeDataAdapter da = new SqlCeDataAdapter(new SqlCeCommand("Select * From tb_RevisionDefinitions",conn));
        DataSet ds = new DataSet("Helper");
        ds.Tables.Add("DefinitionsTable");
        da.Fill(ds.Tables["DefinitionsTable"]);

        // Assign the BindingSource.
        definitionsBindingSource.DataSource = ds.Tables["DefinitionsTable"];
        uxBindingNavigator.BindingSource = this.definitionsBindingSource;

    }
    void assignControlsToSource() 
    {
        uxDescriptionTextBox.DataBindings.Add(new Binding("Text", definitionsBindingSource, "Description", true));
        uxWordPhraseTextBox.DataBindings.Add(new Binding("Text", definitionsBindingSource, "WordPhrase", true));
        uxReferenceTextBox.DataBindings.Add(new Binding("Text", definitionsBindingSource, "Reference", true));
    }

    private void uxUpdateDataButton_Click(object sender, EventArgs e)   
    {
        uxRevisionHelperGroupBox.Enabled = false;
        uxBindingNavigator.Hide();
        uxFormDatabase myNewDisplay = new uxFormDatabase();
        myNewDisplay.FormClosed += delegate { activateGroupBorder(); };
        myNewDisplay.Show();    
    }

    public void activateGroupBorder() 
    {
        uxRevisionHelperGroupBox.Enabled = true;
        uxBindingNavigator.Show();
        refreshBindingSource();    //<<<<<<<<<<<DELEGATE CALLS THIS METHOD
    }

}

以上似乎有效,但我真的必须运行 refreshBindingSource 方法中的所有代码以确保父表单上显示的信息是最新的吗?

The above seems to work but do I really have to run all the code in the method refreshBindingSource to make sure the info displayed on the parent form is up-to-date ?

更新

我遵循了 Amiram 的建议,并传入了我的 BindingSource 以便不必重复父表单中已有的代码.我在一些样板代码中复制了方法 saveToolStripButton_Click;...真的不知道在那个小程序中发生了什么 - 这两行是否足以将信息保存回数据库?

I've followed Amiram's advice and passed in my BindingSource so as not to have to repeat code already in place for the parent form. I've copied in some boiler plate code the method saveToolStripButton_Click; ... really don't know what is going on in that small routine - will those two lines will suffice for saving info back to the database?

public partial class uxFormDatabase : Form
{

    BindingSource rawtableBindingSource = null;

    public uxFormDatabase(BindingSource myPassedSource) 
    {
        InitializeComponent();
        rawtableBindingSource = myPassedSource;

        uxDGVtable.AutoSize = true;
        uxDGVtable.SizeChanged += new EventHandler(uxDGVtable_change);
        dataToDGV();
    }
    public void uxDGVtable_change(object sender, EventArgs e)
    {
        if (uxDGVtable.Width < 1158)
        {
            this.Width = uxDGVtable.Width;
        }
    }

    public void dataToDGV()
    {
        uxrawdataBindingNavigator.BindingSource = this.rawtableBindingSource;
        uxDGVtable.DataSource = this.rawtableBindingSource;
    }

    private void saveToolStripButton_Click(object sender, EventArgs e)
    {
        Validate();
        rawtableBindingSource.EndEdit();
    }

}

推荐答案

如果您对两种表单使用不同的数据源,您别无选择,只能重新加载数据(有一种方法可以使用 sql server 自动执行此操作),但是您如果您使用相同的数据集甚至相同的 BindingSource,则可以避免这种情况,因此刷新将自动发生.

If you used different data source for both forms them you have no choice but to reload data (there is a way to automate that with sql server), but you can avoid that if you'll use the same dataset or even the same BindingSource, so the refresh will happen automatically.

这篇关于刷新winform上显示的绑定数据需要什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆