Click here to Skip to main content
15,881,516 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have MainForm and Form2 is displayed in panel of Mainform.(Looks like MDI form but not MDI as I have heard that MDIForm is bad idea)


When I Check the RowCount Of datagridview1 displayed on Form2 in Mainform.cs or
any other form the datagridview1.RowCount is always one.

I have also found that Form2.IsHandleCreated is always false .Even If I see the Form2 displayed in Mainform panel.

So I have created handle by CreateControl method for Form2 and datagridview1.

but still Form2.IsHandleCreated is false.
datagridview1.ISHandle is false ALWAYS.WHY?WHY?WHY?.
also tried
I have tried a lot but not found any solution for this.


And I know how to use delegate to write values from different thread.
I have written values of Maindatagridview from thread because I can see Its
RowCount is What I have entered(more than one).

So I only want the solution for why RowCount is always one in Child Form2.
This is happening with every child form.
Becasue of this problem I am not able to write values to datagridview from any other
form ,even from Mainform.

Question Improved

//
Code 1
How I have created Form2 In MainForm

C#
private Form CreateForm(ControlsEnum frm)
        {
            switch (frm)
            {
                case ControlsEnum.Form2:
                    return new Form2();
                 case... number of forms
                 default: return null;
            }
         }


C#
public void ShowForm(ControlsEnum frm)
       {

           Form new_Frm = null;

          //If dictionary already contains instance of //Idictionary Interface is used
           if (controls.ContainsKey(frm))
           {

               new_Frm = controls[frm];
           }
           else
           {
               new_Frm = CreateForm(frm);
               controls[ctrl] = new_Frm;
               new_Frm.TopLevel = false;
           }

           new_Frm.Parent = this;
           new_Frm.Dock = DockStyle.Fill;
           new_Frm.BringToFront();

           ShowFormPanel_PNL.Controls.Add(new_ctrl); //this panel is in Mainform
        
            new_ctrl.Show();
      
         ////I have use stack to remove previos form //code deleted

       }



C#
void  Mainform_Load()
{
  //  ShowForm(ControlEnum.Form2);
  if replaced the above line with below code I get results
            f2 = new Form2();
            f2.TopLevel = false;
            ShowFormPanel_PNL.Controls.Add(f2);
            f2.Show();
}


// When Form2 and Mainform displayed. I add dynamically rows to gridview from a button on Form2.
//then I click MainFormbutton1

C#
private void MainFormbutton1_Click(object sender, EventArgs e)
{
           Program.f2 = f2;
           int x= Program.f2.From2GridView.Rows.Count; 
           int y = f2.From2GridView.Rows.Count; 
          // I can see the expected result.
          // but if replaced with ShowForm Rowcount is always 1.
}
Posted
Updated 17-May-14 14:53pm
v3
Comments
DamithSL 16-May-14 23:41pm    
can you update the question with the code how you display or load form2 and how you get the row count?
karthik Udhayakumar 17-May-14 0:29am    
post your code.
DamithSL 17-May-14 0:49am    
where you call CreateForm?
krishpraj123 17-May-14 1:12am    
Sorry That CreateControl in Showform() method is CreateForm
krishpraj123 17-May-14 20:38pm    
Sir the Problem is when I use funtion ShowForm,CreateForm

It is showing new form.
I have created object Program.Form2Obj in main(). //Program.cs
So this object is not getting updated.

When I directly create Form2 on MainForm in Mainforms Load method
I can see the exact rowcount of datagridview of Form2 in MainForm.

code//
MainForm_Load()
{
// replaced ShowForm(ControlEnum.Form2) with
f2 = new Form2();
f2.TopLevel = false;
ShowFormPanel.Controls.Add(f2);
f2.Show();

}

MainFormButton_Click() //button click event in Mainform. Mainform& Form2 both visible
{
Program.Form2Obj = f2;
int x= Program.f2.From2GridView.Rows.Count; //Shows Exact count
int y = f2.From2GridView.Rows.Count;
//when ShowForm(ControlEnum.Form2) used both the counts are one

}

even I tried to return form object method in ShowForm
// gives error when I try to assign it to ohter form object

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