Gridview标题自定义问题

发布于 2024-12-11 14:03:59 字数 1561 浏览 0 评论 0原文

如果我们向gridview添加客户标题,它会添加额外的行吗?

目前,我有一个包含 4 列的 gridview,当我添加自定义标题 gridview 时,它包含 5 行。

我的代码看起来像这样...

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

以及其他三个 TemplateFields...

以这种方式添加标头有任何问题吗?还有其他方法可以添加客户标题而不出现此问题吗? 我想要的输出应该是这样的

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

我现在得到的是

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

如果有想法有人可以帮忙吗? 提前致谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绿萝 2024-12-18 14:03:59

您刚刚向网格视图添加了另一列带有自定义标题的列。如果您想自定义第一列的标题,只需自定义第一个模板字段的标题即可:

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

如果此下拉列表太大或者您想在标题中添加一些附加文本,您始终可以为合并的其他 TemplateFields 创建 HeaderTemplate代码后面的一些列标题(例如“合并”两个第一个标题,gridView 的 ID 为 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);
}

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);
}
江心雾 2024-12-18 14:03:59

好吧,您实际上并没有添加标题行,而是添加了标题列..这基本上只是具有更多控件的另一列..

为什么不将 DropDownList 放置在 GridView 之前,然后您可以在处理 DropDownList_SelectedIndexChanged 事件后面编写一些代码对数据进行排序?

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 ?

指尖上得阳光 2024-12-18 14:03:59

我在没有使用 GridView 中的过滤器的情况下实现了这一点。然后,您可以单独处理事件,并相应地填充 GridView。

您实际添加的是一列,而不是一行。这就是你得到这个结果的原因。

希望这有帮助!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文