473,245 Members | 1,519 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,245 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 5705
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

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

Similar topics

by: hammad.awan_nospam | last post by:
Hello, I am using ASP.NET 2.0. What I have done is nested a gridview inside another column of a gridview using a template data field column declaritively in my web form. Inside this child...
by: Martin | last post by:
Hi, I have a very frustrating problem that I have researched for countless hours to no avail. There are many posts asking very similar things, however none usefull in my situation. I am using VS...
by: H5N1 | last post by:
Hi there My problem is that in when I update GridView row, which is nested into DataList control, I want to refresh also DataList in which the GridView is nested, since after update, trigger in...
by: manuel.ricca | last post by:
Hello, I'm trying to create a table with 2 nested gridviews and then a DetailsView on the right. The DetailsView should show the details of the item selected in the 2nd (nested) GridView: My...
by: Roy | last post by:
Hey all. Below is the nested syntax on how to make a "codeless" nested gridview embedded within another gridviews templatefield column. Only problem is that it loads slow. REAL SLOW. There has to...
by: luvdairish | last post by:
Controls (in order on page) 1.)GridView - each row is a work order w/ a link for details in the last column 2.)DetailsView - when user clicks on link from above GridView, the details are shown...
by: mutt1170 | last post by:
I have a checkbox list nested inside a gridview. The gridview pulls its data from an objectdatasource and lists countries. The nested checkbox list is databound to another object datasource and...
by: sharonrao123 | last post by:
hello all, I have a parent gridview company and in this one a nested gridview people, Is it possible to allow the user to select one row or multiple rows from the people gridview using a check box...
by: RobertTheProgrammer | last post by:
Hi folks, I've got another problem. Basically, I'm trying to use a nested GridView, however the nexted GridView displays no values (even though in debug I'm getting valid values into my DataSet. ...
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.