Scenario:
- A child form which is made visible via a button.
- A delegate is created to run certain code when this child is closed.
- The child form is used to edit the underlying data
- When the child form is closed the latest version of the data should be displayed on any bound controls on the parent form.
Question –
Here is the relevant code attempt:
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
}
}
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 ?
UPDATE
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();
}
}
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.