Reputation: 245
I am trying to update a SQL databound combobox with new information from a new form. After I make the adds/edits and save on the popup form, I want to refresh the combobox with the new info and select the currently added/edited item. Currently, the code below refreshes the list, but will not modify "myID" on the parent form as I thought a reference variable should. How can I most efficiently do this? I have about 20 forms to do a similar type thing.
In form1
int newid = 0;
private void addToolStripMenuItem1_Click(object sender, EventArgs e)
{
CoalSeamsForm csf = new CoalSeamsForm(ref newid);
csf.ShowDialog();
coalSeamsTableAdapter.Fill(well_Information2DataSet.CoalSeams);
coalSeamsBindingSource.Find("CoalSeamID", newid);
}
In form 2
int myID = 0;
public CoalSeamsForm(ref int myId)
{
this.myID = myId;
InitializeComponent();
}
private void CoalSeamsForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (!isOK)
{
if (DialogResult.Yes == MessageBox.Show("Would you like to save the changes?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
{
Save();
DataRowView drv = (DataRowView)coalSeamsBindingSource.Current;
myID = (int)drv["CoalSeamID"];
}
}
}
}
Upvotes: 0
Views: 702
Reputation: 245
Reference parameters only work within the calling method/constructor. Once they are outside of that individual method, they become copies within the second object. To implement, from the suggestions of Steve and others, I used a public property and accessed it from the first form. Also, I used a databound combobox in the posted code, which is a more typical application from me vs. setting the BindingSource.Position property.
In form1
private void addEditCoalSeams(bool isAdd)
{
int? myId=null;
if (!isAdd)
{
myId = (int?)coalSeamsComboBox.SelectedValue;
}
using (CoalSeamsForm csf = new CoalSeamsForm(myId, isAdd))
{
if (DialogResult.OK == csf.ShowDialog())
{
coalSeamsTableAdapter.Fill(well_Information2DataSet.CoalSeams);
coalSeamsComboBox.SelectedValue = csf.coalID;
}
}
}
In CoalSeamsForm
public int? coalID {get; set;}
bool isAdd = false;
public CoalSeamsForm(int? coalId, bool isAdd)
{
this.coalID = coalId;
this.isAdd = isAdd;
InitializeComponent();
}
private void okButton_Click(object sender, EventArgs e)
{
Save();
DataRowView drv = (DataRowView)coalSeamsBindingSource.Current;
this.coalID = (int)drv["CoalSeamID"];
this.DialogResult = DialogResult.OK;
}
Upvotes: 0
Reputation: 216293
There is a problem here. I don't even know if this code compiles. (two vars with the same name?)
int myID = 0;
public CoalSeamsForm(ref int myID)
{
this.myID = 0;
InitializeComponent();
}
However your problem is that you pass by ref the myID inside the form constructor, then your code exit. When you update the var myID in Form_Closing you are no more referencing the var passed inside the constructor. To resolve your problem declare a global property like
public property MyID {get; set;}
then in the constructor
public CoalSeamsForm()
{
this.MyID = 0;
InitializeComponent();
}
update the property when the user clicks a CONFIRM button (not in the form_closing event)
private void ConfirmButton_Click(object sender, EventArgs e)
{
...
this.MyID = (int)drv["CoalSeamID"];
this.DialogResult = DialogResult.OK;
}
and finally use the property value when calling
using(CoalSeamsForm csf = new CoalSeamsForm())
{
if(DialogResult.OK == csf.ShowDialog())
{
coalSeamsTableAdapter.Fill(well_Information2DataSet.CoalSeams);
coalSeamsBindingSource.Find("CoalSeamID", this.MyID);
}
}
Upvotes: 1