Reputation: 31
I'm trying to UPDATE database information through a Form (In WinForms). The DataGridView shows the database info in Form1 (which contains also a button that opens Form2 (the update Form)). To update an information, I have to select a row in the datagrid, which I want to update, and then click on the button (which opens Form2). When the Update Form opens, the textboxes in it should be filled with the DataGridRows information. Now here's where I've been stuck, the textboxes are not being filled (there is no error). What am I doing wrong ?
Here's the code I'm using:
MainForm getMainForm = new MainForm();
private void EditMemberForm_Load(object sender, EventArgs e)
{
DataGridViewCell cell = null;
foreach (DataGridViewCell selectedCell in getMainForm.MembersGridView.SelectedCells)
{
cell = selectedCell;
break;
}
if (cell != null)
{
DataGridViewRow row = cell.OwningRow;
EditFirstNameTextBox.Text = row.Cells["FirstNameColumn"].Value.ToString();
EditLastNameTextBox.Text = row.Cells["LastNameColumn"].Value.ToString();
EditPersonalIdTextBox.Text = row.Cells["PersonalIdColumn"].Value.ToString();
EditCityComboBox.Text = row.Cells["CityColumn"].Value.ToString();
EditPhoneNumberTextBox.Text = row.Cells["PhoneNumberColumn"].Value.ToString();
}
}
Upvotes: 0
Views: 1577
Reputation: 646
I had a similar problem recently and I solved it like this.
Form1:
public void NotifyMe(string s, string s2, string s3, string s4, string s5)
{
if (textBox1.InvokeRequired)
{
textBox1.Invoke((MethodInvoker)delegate { textBox1.Text = s; });
}
else
{
textBox1.Text = s;
}
//...
}
Open Form2 from Form1 like this:
using (Form2 form = new Form2())
{
// passing this in ShowDialog will set the .Owner
// property of the child form
form.ShowDialog(this);
}
Form2:
parent = (Form1)this.Owner;
parent.NotifyMe(s, s2, s3, s4, s5);
Upvotes: 0
Reputation: 31
I figured this out.
I used this code in the button that opens the UpdateForm (from the MainForm).
EditMemberForm getEditMemberForm = new EditMemberForm();
DataGridViewCell cell = null;
foreach (DataGridViewCell selectedCell in MembersGridView.SelectedCells)
{
cell = selectedCell;
break;
}
if (cell != null)
{
DataGridViewRow row = cell.OwningRow;
getEditMemberForm.EditFirstNameTextBox.Text = row.Cells["FirstNameColumn"].Value.ToString();
getEditMemberForm.EditLastNameTextBox.Text = row.Cells["LastNameColumn"].Value.ToString();
getEditMemberForm.EditPersonalIdTextBox.Text = row.Cells["PersonalIdColumn"].Value.ToString();
getEditMemberForm.EditCityComboBox.Text = row.Cells["CityColumn"].Value.ToString();
getEditMemberForm.EditPhoneNumberTextBox.Text = row.Cells["PhoneNumberColumn"].Value.ToString();
}
getEditMemberForm.ShowDialog();
This works perfectly. Thanks for anyone who tried to help, cheers :)!
Upvotes: 0
Reputation: 323
Instead of creating a new MainForm, pass your selected object from your mainform to the Form2 when you click on the button...
Example :
private void Form2Button_Click(object sender, EventArgs e){
Form f=new Form2(dataGridView.SelectedRow);
f.show();
}
And in you Form2 constructor:
object myRow;
public Form2(object myRow){
this.myRow=myRow
}
Upvotes: 0
Reputation: 63105
MainForm getMainForm = new MainForm();
problem is you are creating new main form and try to get selected row from that newly created main form.
When you create EditMemberForm
you can pass values as parameter to the EditMemberForm
Upvotes: 1