刷新winform上显示的绑定数据需要什么?

时间:2012-07-15 14:39:38

标签: c# winforms visual-studio-2010 sql-server-ce

情景:

  • 通过按钮显示的子表单。
  • 创建一个委托,以便在关闭此子项时运行某些代码。
  • 子表单用于编辑基础数据
  • 关闭子表单时,应在父表单上的任何绑定控件上显示最新版本的数据。

问题 -

以下是相关的代码尝试:

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中的所有代码以确保父表单上显示的信息是最新的吗?

更新

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

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

}

1 个答案:

答案 0 :(得分:0)

如果你为两种表单使用了不同的数据源,你别无选择,只能重新加载数据(有一种方法可以使用sql server自动执行),但如果你使用相同的数据集甚至是相同的BindingSource,因此刷新将自动发生。