Finding the handle of a nested GridView

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RobertTheProgrammer
    New Member
    • Aug 2007
    • 58

    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 OnSelectedIndex Changed event set on it. This triggers just fine, but within the codebehind of the OnSelectedIndex Changed 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):

    Code:
    <asp:GridView ID="EmployeeGroupsGridView" runat="server" />
       <Columns>
          <asp:TemplateField HeaderText="ID"> 
             <ItemTemplate> 
                <asp:Label ID="employee_group_id" runat="server" Text='<%# Bind("employee_group_id") %>'></asp:Label> 
             </ItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="Employees">
             <ItemTemplate> 
                <asp:GridView ID="EmployeeGroupEmployeesGridView" runat="server"
                        OnRowCommand="EmployeeGroupEmployeesGridView_RowCommand" 
                        OnRowDataBound="EmployeeGroupEmployeesGridView_RowDataBound">
                   <Columns>
                      <asp:TemplateField>
                         <ItemTemplate> 
                            <asp:Label ID="recno" runat="server" Text='<%# Bind("recno") %>'></asp:Label> 
                         </ItemTemplate>
                      </asp:TemplateField>
                      <asp:TemplateField>
                         <ItemTemplate> 
                            <asp:DropDownList id="employee_name" DataSourceID="SqlDataSourceEmployeeNamesPlus" Runat="Server" 
                                   OnSelectedIndexChanged="EmployeeName_SelectedIndexChanged" AutoPostBack="true"
                                   DataTextField="employee_name" DataValueField="employee_name" 
                                   SelectedValue='<%# Bind("employee_name") %>'/>
                         </ItemTemplate>
                      </asp:TemplateField>
                   </Columns>
                </asp:GridView>
             </EditItemTemplate>
          </asp:TemplateField>
       </Columns>
    </asp:GridView>
    Down in the codebehind for EmployeeName_Se lectedIndexChan ged, I can get the row that changed and such with the following:

    Code:
    DropDownList ddl = (DropDownList)sender;
    Control senderControl = (Control)sender;
    GridViewRow row = (GridViewRow)senderControl.NamingContainer;
    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.

    Robert
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    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?

    Comment

    • RobertTheProgrammer
      New Member
      • Aug 2007
      • 58

      #3
      But *how* do I look at the cell?

      -R

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Uhh, really easy...just give the row and column index?
        [code=c#]
        ControlCollecti on SearchThroughTh is = myBigGridView.R ows[RowIWant].Cells[ColumnIWant].Controls;
        [/code]

        Comment

        • RobertTheProgrammer
          New Member
          • Aug 2007
          • 58

          #5
          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 ControlCollecti on. In any case, I figured out a way to do it. In my codebehind, I have:

          Code:
          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 myNestedGridVie w variable and I can manipulate it however I want.

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

          Robert

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Well I am not sure why you had trouble looping throught the controlcollecti on?
            [code=c#]
            ControlCollecti on SearchThroughTh is = gvOverdueSites. Rows[0].Cells[0].Controls;
            for (int i = 0; i < SearchThroughTh is.Count; i++)
            {
            if (SearchThroughT his[i].GetType() == GridView)
            {
            GridView myGridView = SearchThroughTh is[i];
            //do stuff
            }
            }
            [/code]

            Comment

            • RobertTheProgrammer
              New Member
              • Aug 2007
              • 58

              #7
              Just chalk it up to my own ignorance about how to use the ControlCollecti on object. I'm still learning.

              -R

              Comment

              Working...