1

If we add a customer header to the gridview, will it add extra row?

Currently I have a gridview with four columns and, when I add a custom header gridview coming with a five rows.

My code looks like this...

<asp:TemplateField HeaderText="" ItemStyle-Width="4%" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
          <HeaderTemplate>
              <asp:DropDownList ID="Select" runat="server">
                <asp:ListItem>Country</asp:ListItem>
                <asp:ListItem>Region</asp:ListItem>
                <asp:ListItem>Title</asp:ListItem>
              </asp:DropDownList>
          </HeaderTemplate>
</asp:TemplateField>  

 <asp:TemplateField HeaderText="" ItemStyle-Width="4%" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
        <ItemTemplate>
             <asp:HiddenField ID="Id"  Value='<%#Eval("id")%>' runat="server" />  
             <asp:Literal ID="ltrImage" runat="server"></asp:Literal>  
        </ItemTemplate>
 </asp:TemplateField>

and three other TemplateFields...

Is there any problem of adding header this way? Any other way to add customer header without having this issue? My desired output should look like this

Search Result   Search By (Dropdownlist)
Data column1    Data column2             Data column3     Data column4

The one I am getting now is

Sort By (Dropdownlist)  
                       Data column1   Data column2  Data column3    Data column4

Could anyone help if have an idea? Thanks in advance

3 Answers 3

1

You just added another column with custom header to your gridview. If you want to customize a header of the first column, just customize the header of your first template field:

 <asp:TemplateField HeaderText="" ItemStyle-Width="4%" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
         <HeaderTemplate>
              <asp:DropDownList ID="Select" runat="server">
                <asp:ListItem>Country</asp:ListItem>
                <asp:ListItem>Region</asp:ListItem>
                <asp:ListItem>Title</asp:ListItem>
              </asp:DropDownList>
         </HeaderTemplate>
        <ItemTemplate>
             <asp:HiddenField ID="Id"  Value='<%#Eval("id")%>' runat="server" />  
             <asp:Literal ID="ltrImage" runat="server"></asp:Literal>  
        </ItemTemplate>
 </asp:TemplateField>

If this dropdownlist is too big or if you want add some additional text in the header, you can always create HeaderTemplate for other TemplateFields of merge some of the column's headers in code behind (example for "merging" two first headers, Id of the gridView is gridView1):

protected void gridView1_PreRender(object sender, EventArgs e)
{
    int indexOfColumnToSpan = 0;
    int indexOfColumnToRemoveHeader = 1;
    gridView1.HeaderRow.Cells[indexOfColumnToSpan].ColumnSpan = 2;  
    gridView1.HeaderRow.Cells.RemoveAt(indexOfColumnToRemoveHeader);
}
Sign up to request clarification or add additional context in comments.

Comments

0

well, you have not actually added a header row, but a header column .. which is basically just another column with more controls ..

why dont you place your DropDownList before your GridView, and then you can write some code behind handling the DropDownList_SelectedIndexChanged event to sort the Data ?

Comments

0

I have implemented this without using the filters within the GridView. Then you can handle the events separately, and populate the GridView accordingly.

What you are actually adding is a column, instead of a row. That's why you are getting that result.

Hope this helps!

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.