471,088 Members | 1,295 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,088 software developers and data experts.

Finding the handle of a nested GridView

Hi folks,

Here's a weird problem...

I have a nested GridView setup (i.e. a GridView within a GridView), and within the nested GridView I have a DropDownList item which has the OnSelectedIndexChanged event set on it. This triggers just fine, but within the codebehind of the OnSelectedIndexChanged event, I need to scan through all the entries in the nested GridView (to see if the user changed a value to an already existing value in the list). My problem is that I can't seem to get a handle on the nested GridView.

Here's an example (simplified from my actual code for readability):

Expand|Select|Wrap|Line Numbers
  1. <asp:GridView ID="EmployeeGroupsGridView" runat="server" />
  2.    <Columns>
  3.       <asp:TemplateField HeaderText="ID"> 
  4.          <ItemTemplate> 
  5.             <asp:Label ID="employee_group_id" runat="server" Text='<%# Bind("employee_group_id") %>'></asp:Label> 
  6.          </ItemTemplate>
  7.       </asp:TemplateField>
  8.       <asp:TemplateField HeaderText="Employees">
  9.          <ItemTemplate> 
  10.             <asp:GridView ID="EmployeeGroupEmployeesGridView" runat="server"
  11.                     OnRowCommand="EmployeeGroupEmployeesGridView_RowCommand" 
  12.                     OnRowDataBound="EmployeeGroupEmployeesGridView_RowDataBound">
  13.                <Columns>
  14.                   <asp:TemplateField>
  15.                      <ItemTemplate> 
  16.                         <asp:Label ID="recno" runat="server" Text='<%# Bind("recno") %>'></asp:Label> 
  17.                      </ItemTemplate>
  18.                   </asp:TemplateField>
  19.                   <asp:TemplateField>
  20.                      <ItemTemplate> 
  21.                         <asp:DropDownList id="employee_name" DataSourceID="SqlDataSourceEmployeeNamesPlus" Runat="Server" 
  22.                                OnSelectedIndexChanged="EmployeeName_SelectedIndexChanged" AutoPostBack="true"
  23.                                DataTextField="employee_name" DataValueField="employee_name" 
  24.                                SelectedValue='<%# Bind("employee_name") %>'/>
  25.                      </ItemTemplate>
  26.                   </asp:TemplateField>
  27.                </Columns>
  28.             </asp:GridView>
  29.          </EditItemTemplate>
  30.       </asp:TemplateField>
  31.    </Columns>
  32. </asp:GridView>
Down in the codebehind for EmployeeName_SelectedIndexChanged, I can get the row that changed and such with the following:

Expand|Select|Wrap|Line Numbers
  1. DropDownList ddl = (DropDownList)sender;
  2. Control senderControl = (Control)sender;
  3. GridViewRow row = (GridViewRow)senderControl.NamingContainer;
  4. GridView gv = (GridView)row.FindControl("EmployeeGroupEmployeesEditGridView");
But the last call to get the GridView item so I can scan through all the records always returns null. I'm sure it's because the code is looking for the GridView within the row and, of course, it doesn't exist there. It exists in the outer GridView's row. So how do I get a handle on this inner GridView?

Many thanks for any help.

Jun 23 '08 #1
6 5542
7,872 Expert 4TB
couldn't you look at the "cell" at the given row/column, then look at the controls in that collection for the one that is a GridView?
Jun 23 '08 #2
But *how* do I look at the cell?

Jun 23 '08 #3
7,872 Expert 4TB
Uhh, really easy...just give the row and column index?
Expand|Select|Wrap|Line Numbers
  1. ControlCollection SearchThroughThis = myBigGridView.Rows[RowIWant].Cells[ColumnIWant].Controls;
Jun 23 '08 #4
Well, I couldn't quite get your solution to work. I was having problems (1) trying to figure out what row I was on, but I got that solved using a ViewState variable and (2) how did I get to the nested GridView from the ControlCollection. In any case, I figured out a way to do it. In my codebehind, I have:

Expand|Select|Wrap|Line Numbers
  1. GridView myNestedGridView = (GridView)senderControl.Parent.Parent.Parent.Parent.Parent.FindControl("EmployeeGroupEmployeesEditGridView");
Note the multiple Parent references, which I had to figure out how many by trial and error. This puts the nested GridView into the myNestedGridView variable and I can manipulate it however I want.

Thanks for taking the time to reply and thanks for reading.

Jun 23 '08 #5
7,872 Expert 4TB
Well I am not sure why you had trouble looping throught the controlcollection?
Expand|Select|Wrap|Line Numbers
  1. ControlCollection SearchThroughThis = gvOverdueSites.Rows[0].Cells[0].Controls;
  2.         for (int i = 0; i < SearchThroughThis.Count; i++)
  3.         {
  4.             if (SearchThroughThis[i].GetType() == GridView)
  5.             {
  6.                 GridView myGridView = SearchThroughThis[i];
  7.                 //do stuff
  8.             }
  9.         }
Jun 23 '08 #6
Just chalk it up to my own ignorance about how to use the ControlCollection object. I'm still learning.

Jun 23 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by hammad.awan_nospam | last post: by
reply views Thread by manuel.ricca | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.