Nested GridView not binding

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

    Nested GridView not binding

    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):

    Code:
    <asp:GridView ID="GridViewMain" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999"
        BorderStyle="None" Font-Names="Arial" BorderWidth="1px" CellPadding="3" DataSourceID="SqlDataSourceTrainingUnits"
        GridLines="Vertical" DataKeyNames="unit_id" OnRowUpdating="Validate_Data" OnRowCommand="grd_RowCommand"
        ShowFooter="True" OnRowDataBound="GridViewMain_RowDataBound">
        <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
        <Columns>
          <asp:TemplateField HeaderText="ID"> 
            <EditItemTemplate> 
              <asp:TextBox ID="unit_id" runat="server" Width="20px" Text='<%# Bind("unit_id") %>'></asp:TextBox> 
            </EditItemTemplate> 
            <ItemTemplate> 
              <asp:Label ID="unit_id" runat="server" Text='<%# Bind("unit_id") %>'></asp:Label> 
            </ItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="Name">
            <EditItemTemplate> 
              <asp:TextBox ID="unit_name" runat="server" Text='<%# Bind("unit_name") %>'></asp:TextBox> 
            </EditItemTemplate> 
            <FooterTemplate> 
              <asp:TextBox ID="unit_name" runat="server" ></asp:TextBox> 
            </FooterTemplate> 
            <ItemTemplate> 
              <asp:Label ID="unit_name" runat="server" Text='<%# Bind("unit_name") %>'></asp:Label> 
            </ItemTemplate>
          </asp:TemplateField>
    
    
          <asp:TemplateField HeaderText="OtherStuff">
            <ItemTemplate> 
              <asp:GridView ID="GridViewNested" runat="server" AutoGenerateColumns="False" BackColor="White"
                BorderColor="#999999" BorderStyle="None" Font-Names="Arial" BorderWidth="1px" CellPadding="3" 
                DataSourceID='<%# GetDocuments(Convert.ToInt32(Eval("unit_id"))) %>'
                GridLines="Vertical" >
                <Columns>
                    <asp:BoundField DataField="doc_name" HeaderText="Document Name" />
                    <asp:BoundField DataField="version" HeaderText="Version" />
                </Columns>
                <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
                <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
                <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
                <AlternatingRowStyle BackColor="Gainsboro" />
              </asp:GridView>
            </ItemTemplate>
          </asp:TemplateField>
    
    
        </Columns>
                <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
                <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
                <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
                <AlternatingRowStyle BackColor="Gainsboro" />
    </asp:GridView>
    As you can see, the datasource for GridViewMain is an SQL data source and I have declared "unit_id" as the "DataKeyNam es" 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:

    Code:
    protected DataTable GetDocuments(int key)
    {
        System.Data.DataTable tbl = new DataTable();
        DataColumn doc_namecol = new DataColumn("doc_name");
        tbl.Columns.Add(doc_namecol);
        DataColumn versioncol = new DataColumn("version");
        tbl.Columns.Add(versioncol);
    
        System.Data.OleDb.OleDbConnection ole1 = new System.Data.OleDb.OleDbConnection();
        ole1.ConnectionString = ConfigurationSettings.AppSettings["Training Connection String"];
        OleDbCommand cmd = ole1.CreateCommand();
        ole1.Open();
        OleDbDataReader dr;
        cmd.CommandText = "select doc_name, version, effective_date from training_documents where unit_id = " + key.ToString();
    
        dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            if ((!dr.IsDBNull(0)) && (!dr.IsDBNull(1))))
            {
                DataRow drow = tbl.NewRow();
                drow["doc_name"] = dr.GetString(0).Trim();
                drow["version"] = dr.GetString(1).Trim();
                tbl.Rows.Add(drow);
            }
        }
    
        dr.Close();
        cmd.Dispose();
        ole1.Close();
        ole1.Dispose();
    
        return tbl;
    }
    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
  • nateraaaa
    Recognized Expert Contributor
    • May 2007
    • 664

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

    Comment

    • RobertTheProgrammer
      New Member
      • Aug 2007
      • 58

      #3
      It's right there in the code I listed:

      In the nested GridView, the datasource is in the line:
      Code:
      DataSourceID='<%# GetDocuments(Convert.ToInt32(Eval("unit_id"))) %>'
      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

      Comment

      • nateraaaa
        Recognized Expert Contributor
        • May 2007
        • 664

        #4
        Originally posted by RobertTheProgra mmer
        It's right there in the code I listed:

        In the nested GridView, the datasource is in the line:
        Code:
        DataSourceID='<%# GetDocuments(Convert.ToInt32(Eval("unit_id"))) %>'
        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.


        Nathan

        Comment

        Working...