Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends,


i have two grid one is parent and second is child. now i want to access child gridview when button click i try something but it display null. here what i tried


C#
protected void BtnSet_Click(object sender, EventArgs e)
    {
        GridView GrdRights = (GridView)this.GrdParent.FindControl("GrdRightDetail");
    }


It display null ..can anyone tell me how to get child grid view on button click.
Posted
Comments
Is the button "BtnSet" inside the parent GridView ?
[no name] 19-Nov-12 9:44am    
You have to loop through the controls in the table and see if one of the controls is of the type grid.
Yatin chauhan 19-Nov-12 9:59am    
@Tadit Dash: Button is in Child Gridview.
[no name] 19-Nov-12 10:51am    
if you use the property hasChildren to make your search recursive then you can find the correct control

Here is some code you can use, in this code I compare the control by the control type but if you want you can change is to compare by the control name and it works effectively.
C#
private void button1_Click(object sender, EventArgs e)
       {
           object childgrid;

           childgrid =  getControlByType(this, typeof(DataGridView).ToString());
           childgrid =  getControlByName(this, dataGridView1.Name);
       }

       private object getControlByType(Control Main, string controlType)
       {
           if (Main.HasChildren)
           {
               foreach (var control in Main.Controls)
               {
                   if (control.GetType().ToString() == controlType) return control;
               }
           }

           return null;
       }

       private object getControlByName(Control Main, string controlName)
       {
           if (Main.HasChildren)
           {
               foreach (var control in Main.Controls)
               {
                   var ctl = (Control) control;
                   object ctl2 = null;

                   if (ctl.GetType().ToString() == typeof (DataGridView).ToString())
                   {
                       ctl2 = ctl;
                       if (ctl.Name == controlName)
                       {return ctl2;}
                       ctl2 = null;
                   }

                   if (ctl.GetType().ToString() == typeof(Button).ToString())
                   {
                       ctl2 = ctl;
                       if (ctl.Name == controlName)
                       { return ctl2; }
                       ctl2 = null;
                   }

                   if (ctl.HasChildren)
                   {
                     ctl2 =  getControlByName(ctl, controlName);
                   }

                   if (ctl2 != null)
                       return ctl2;

               }
           }

           return null;
       }
 
Share this answer
 
v4
Use this one

int SomeRowIndex=0;
GridView GrdRights = this.GrdParent.Rows(SomeRowIndex).FindControl("GrdRightDetail") as GridView;

Thats it
 
Share this answer
 

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