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

Nested GridView not binding

P: 58
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. It's probably a binding issue somewhere, but I'm not sure where.

Here's the ASP (slightly edited to remove verbosity):

Expand|Select|Wrap|Line Numbers
  1. <asp:GridView ID="GridViewMain" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999"
  2.     BorderStyle="None" Font-Names="Arial" BorderWidth="1px" CellPadding="3" DataSourceID="SqlDataSourceTrainingUnits"
  3.     GridLines="Vertical" DataKeyNames="unit_id" OnRowUpdating="Validate_Data" OnRowCommand="grd_RowCommand"
  4.     ShowFooter="True" OnRowDataBound="GridViewMain_RowDataBound">
  5.     <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
  6.     <Columns>
  7.       <asp:TemplateField HeaderText="ID"> 
  8.         <EditItemTemplate> 
  9.           <asp:TextBox ID="unit_id" runat="server" Width="20px" Text='<%# Bind("unit_id") %>'></asp:TextBox> 
  10.         </EditItemTemplate> 
  11.         <ItemTemplate> 
  12.           <asp:Label ID="unit_id" runat="server" Text='<%# Bind("unit_id") %>'></asp:Label> 
  13.         </ItemTemplate>
  14.       </asp:TemplateField>
  15.       <asp:TemplateField HeaderText="Name">
  16.         <EditItemTemplate> 
  17.           <asp:TextBox ID="unit_name" runat="server" Text='<%# Bind("unit_name") %>'></asp:TextBox> 
  18.         </EditItemTemplate> 
  19.         <FooterTemplate> 
  20.           <asp:TextBox ID="unit_name" runat="server" ></asp:TextBox> 
  21.         </FooterTemplate> 
  22.         <ItemTemplate> 
  23.           <asp:Label ID="unit_name" runat="server" Text='<%# Bind("unit_name") %>'></asp:Label> 
  24.         </ItemTemplate>
  25.       </asp:TemplateField>
  26.  
  27.  
  28.       <asp:TemplateField HeaderText="OtherStuff">
  29.         <ItemTemplate> 
  30.           <asp:GridView ID="GridViewNested" runat="server" AutoGenerateColumns="False" BackColor="White"
  31.             BorderColor="#999999" BorderStyle="None" Font-Names="Arial" BorderWidth="1px" CellPadding="3" 
  32.             DataSourceID='<%# GetDocuments(Convert.ToInt32(Eval("unit_id"))) %>'
  33.             GridLines="Vertical" >
  34.             <Columns>
  35.                 <asp:BoundField DataField="doc_name" HeaderText="Document Name" />
  36.                 <asp:BoundField DataField="version" HeaderText="Version" />
  37.             </Columns>
  38.             <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
  39.             <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
  40.             <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
  41.             <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
  42.             <AlternatingRowStyle BackColor="Gainsboro" />
  43.           </asp:GridView>
  44.         </ItemTemplate>
  45.       </asp:TemplateField>
  46.  
  47.  
  48.     </Columns>
  49.             <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
  50.             <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
  51.             <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
  52.             <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
  53.             <AlternatingRowStyle BackColor="Gainsboro" />
  54. </asp:GridView>
  55.  
As you can see, the datasource for GridViewMain is an SQL data source and I have declared "unit_id" as the "DataKeyNames" parameter, which will be used in the nested GridView. Down in the nested GridView, called GridViewNested, the data source calls a method in the codebehind called GetDocuments.

Code for GetDocuments in the codebehind is as follows:

Expand|Select|Wrap|Line Numbers
  1. protected DataTable GetDocuments(int key)
  2. {
  3.     System.Data.DataTable tbl = new DataTable();
  4.     DataColumn doc_namecol = new DataColumn("doc_name");
  5.     tbl.Columns.Add(doc_namecol);
  6.     DataColumn versioncol = new DataColumn("version");
  7.     tbl.Columns.Add(versioncol);
  8.  
  9.     System.Data.OleDb.OleDbConnection ole1 = new System.Data.OleDb.OleDbConnection();
  10.     ole1.ConnectionString = ConfigurationSettings.AppSettings["Training Connection String"];
  11.     OleDbCommand cmd = ole1.CreateCommand();
  12.     ole1.Open();
  13.     OleDbDataReader dr;
  14.     cmd.CommandText = "select doc_name, version, effective_date from training_documents where unit_id = " + key.ToString();
  15.  
  16.     dr = cmd.ExecuteReader();
  17.     while (dr.Read())
  18.     {
  19.         if ((!dr.IsDBNull(0)) && (!dr.IsDBNull(1))))
  20.         {
  21.             DataRow drow = tbl.NewRow();
  22.             drow["doc_name"] = dr.GetString(0).Trim();
  23.             drow["version"] = dr.GetString(1).Trim();
  24.             tbl.Rows.Add(drow);
  25.         }
  26.     }
  27.  
  28.     dr.Close();
  29.     cmd.Dispose();
  30.     ole1.Close();
  31.     ole1.Dispose();
  32.  
  33.     return tbl;
  34. }
  35.  
I've run the above code in debug mode and tbl is being populated with good data, but for unknown reasons, it's not being displayed in the nested GridView. (The main/parent GridView displays just fine).

Any help is greatly appreciated.

Robert
May 21 '08 #1
Share this Question
Share on Google+
3 Replies


nateraaaa
Expert 100+
P: 663
Are you assigning the datatable as the datasource for your nested grid view? Please post the code where you assign the datasource for the nested grid view.

Nathan
May 27 '08 #2

P: 58
It's right there in the code I listed:

In the nested GridView, the datasource is in the line:
Expand|Select|Wrap|Line Numbers
  1. DataSourceID='<%# GetDocuments(Convert.ToInt32(Eval("unit_id"))) %>'
  2.  
And I have shown the full method for GetDocuments.

In any case, the solution was not forthcoming so I found another way to solve my problem. I assigned the datasource in the codebehind instead.

Thanks anyway.

Robert
May 28 '08 #3

nateraaaa
Expert 100+
P: 663
It's right there in the code I listed:

In the nested GridView, the datasource is in the line:
Expand|Select|Wrap|Line Numbers
  1. DataSourceID='<%# GetDocuments(Convert.ToInt32(Eval("unit_id"))) %>'
  2.  
And I have shown the full method for GetDocuments.

In any case, the solution was not forthcoming so I found another way to solve my problem. I assigned the datasource in the codebehind instead.

Thanks anyway.

Robert
I believe that gridview DataSourceId expects the value to be set to an instance of SqlDataSource (or other data source type such as ODBC). You were trying to set the DataSourceId = to a datatable. In the code behind this is valid because to set the datasource property of the gridview = to a datatable. Here is a helpful link on binding data usig datasource controls.
http://msdn.microsoft.com/en-us/library/ms228089.aspx

Nathan
May 28 '08 #4

Post your reply

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