Click here to Skip to main content
13,139,045 members (54,269 online)
Rate this:
 
Please Sign up or sign in to vote.
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


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 19-Nov-12 3:08am
Comments
Tadit Dash 19-Nov-12 9:39am
   
Is the button "BtnSet" inside the parent GridView ?
Mathlab 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.
Mathlab 19-Nov-12 10:51am
   
if you use the property hasChildren to make your search recursive then you can find the correct control
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
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;
       }
  Permalink  
v4
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Use this one

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

Thats it
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web01 | 2.8.170915.1 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100