1

In web application, I am trying to bind the Header Template of the gridivew, but i am not able to bind the data to gridview header.

   <asp:GridView ID ="grdInner" runat ="server" AutoGenerateColumns ="false" >
       <Columns >
          <asp:TemplateField >
             <HeaderTemplate >
          <asp:Label ID="lblHeader" runat="server" Text='<%# Eval("title") %>'></asp:Label>     
              </HeaderTemplate>
              <ItemTemplate >
                 <asp:Label ID ="lblDesc" runat ="server" Text ='<%# Eval("description") %>'></asp:Label>
              </ItemTemplate>
          </asp:TemplateField> 
       </Columns>
     </asp:GridView>
1
  • 1
    You can't data bind to the Header because there is only one. You can only data bind to the repeating Item. Commented Jun 17, 2012 at 18:18

2 Answers 2

5

Following code achieves the same things

<asp:GridView runat="server" ID="gridView" onrowdatabound="gridView_OnRowDataBound" AutoGenerateColumns="false">
<columns>
    <asp:TemplateField>
        <HeaderTemplate><asp:Label runat="server" ID="lblHeader"></asp:Label></HeaderTemplate>
    </asp:TemplateField>
</columns>
</asp:GridView>

You can set label text in OnRowDataBound event

 protected void gridView_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
            ((Label)e.Row.FindControl("lblHeader") as Label).Text = "Your Data here";
    }
Sign up to request clarification or add additional context in comments.

Comments

0

Another way to do this that is fairly simple if you want to bind the DataTable column headers to the Gridview headers (for example, you are generating DataTables where the column names can be different and you want these dynamic names to be the GridView header column names).

DataTable dt = // Call your DataTable here.

    // Get your column names here.
    string nutrient = dt.Columns[2].ColumnName;
    string nutrient1 = dt.Columns[3].ColumnName;
    string nutrient2 = dt.Columns[4].ColumnName;
    string nutrient3 = dt.Columns[5].ColumnName;
    string nutrient4 = dt.Columns[6].ColumnName;
    string nutrient5 = dt.Columns[7].ColumnName;
    
    GridView1.DataSource = dt;
    GridView1.DataBind();

    if (GridView1.Rows.Count > 0)
    {
        GridView1.HeaderRow.Cells[2].Text = nutrient;
        GridView1.HeaderRow.Cells[3].Text = nutrient1; 
        GridView1.HeaderRow.Cells[4].Text = nutrient2;
        GridView1.HeaderRow.Cells[5].Text = nutrient3;
        GridView1.HeaderRow.Cells[6].Text = nutrient4;
        GridView1.HeaderRow.Cells[7].Text = nutrient5;            
    } 

You can also do the same thing with the footer row using GridView1.FooterRow.Cells[x].Text = ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.