Click here to Skip to main content
15,899,025 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,
I am using MDI parent form in C#.There is a toolstrip on that MDI parent form.On toolstrip, there are button1 and button2. On button1 click open form1 and button1's back color is changed. And button2 click open form2 and button2's back color is change. That's OK.But there is a datagridview on form1, when I double click one row in datagridview, I want to close form1 and open form2 and change back color of toolstrip button2. Because I want to show related detail data of one row in datagridview of form1 in form2's gridview. How can I try it.Please help me. Thanks you all.
Posted
Comments
Alexander Dymshyts 7-Oct-13 7:09am    
Can you show your code?

1 solution

First you need to expose the DataGridView Control on both MdiChild Forms so the MdiParent Form can access them: so the first MdiChildForm's code might look like this:
C#
// we expose the DataGridView by creating a public Property
public DataGridView f1DGView { get; set; }

private void MdiChildForm1_Load(object sender, EventArgs e)
{
    f1DGView = dataGridView1;
}
The second MdiChildForm's code will be identical, except for the DataGridView name.

Then, in the MdiParentForm:
// to keep track of the current Row
// when the DataGridView on either MdiCHildForm gets a double-click
int currentDGVRow;

// create instances of the MdiChildForms
private MdiChildForm1 = new MdiChildForm1();
private MdiChildForm2 = new MdiChildForm2();

private void MdiParent_Load(object sender, EventArgs e)
{
    MdiChildForm1.MdiParent = this;
    MdiChildForm2.MdiParent = this;
    //
    // both MdiChild Forms must be shown once to allow access to
    // their internal DataGridView Control
    MdiChildForm1.Show();
    MdiChildForm2.Show();
    MdiChildForm2.Hide();
    //
    // handle the CellDoubleClick Event on both ChildMdiForms
    MdiChildForm1.f1DGView.CellDoubleClick += f1DGView_CellDoubleClick;
    MdiChildForm2.f2DGView.CellDoubleClick += f2DGView_CellDoubleClick;
    //
}

private void f1DGView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    // get the row where the double-click occurred
    currentDGVRow = e.RowIndex;

    // set the ToolStrip buttons background color
    button1.BackColor = Color.Ivory;
    button2.BackColor = Color.Snow;

    // hide the current MdiChildForm and show the other MdiChildForm
    MdiChildForm1.Hide();
    MdiChildForm2.Show();

    // select the same row in the other MdiChildForm which is now visible
    MdiChildForm2.f2DGView.Rows[currentDGVRow].Selected = true;
}

private void f2DGView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    currentDGVRow = e.RowIndex;

    button1.BackColor = Color.Snow;
    button2.BackColor = Color.Ivory;

    MdiChildForm2.Hide();
    MdiChildForm1.Show();

    MdiChildForm1.f1DGView.Rows[currentDGVRow].Selected = true;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900