By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,491 Members | 3,230 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,491 IT Pros & Developers. It's quick & easy.

Finding the handle of a nested GridView

P: 58
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>
  33.  
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");
  5.  
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.

Robert
Jun 23 '08 #1
Share this Question
Share on Google+
6 Replies


Plater
Expert 5K+
P: 7,872
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

P: 58
But *how* do I look at the cell?

-R
Jun 23 '08 #3

Plater
Expert 5K+
P: 7,872
Uhh, really easy...just give the row and column index?
Expand|Select|Wrap|Line Numbers
  1. ControlCollection SearchThroughThis = myBigGridView.Rows[RowIWant].Cells[ColumnIWant].Controls;
  2.  
Jun 23 '08 #4

P: 58
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");
  2.  
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.

Robert
Jun 23 '08 #5

Plater
Expert 5K+
P: 7,872
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.         }
  10.  
Jun 23 '08 #6

P: 58
Just chalk it up to my own ignorance about how to use the ControlCollection object. I'm still learning.

-R
Jun 23 '08 #7

Post your reply

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