Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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 at 19-Nov-12 9:39am
   
Is the button "BtnSet" inside the parent GridView ?
Mathlab at 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 at 19-Nov-12 9:59am
   
@Tadit Dash: Button is in Child Gridview.
Mathlab at 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 2

Use this one
 
int SomeRowIndex=0;
GridView GrdRights = this.GrdParent.Rows(SomeRowIndex).FindControl("GrdRightDetail") as GridView;
 
Thats it
  Permalink  
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

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

  Print Answers RSS
0 CPallini 385
1 Sergey Alexandrovich Kryukov 262
2 OriginalGriff 200
3 George Jonsson 134
4 Richard MacCutchan 110
0 OriginalGriff 6,344
1 Sergey Alexandrovich Kryukov 5,890
2 CPallini 5,175
3 George Jonsson 3,559
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2014
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