设想:
- 通过按钮可见的子窗体。
- 当这个孩子关闭时,会创建一个委托来运行某些代码。
- 子窗体用于编辑底层数据
- 当子窗体关闭时,最新版本的数据应显示在父窗体上的任何绑定控件上。
问题 -
这是相关的代码尝试:
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();
}
}