vb.net 将复选框添加到 gridview 的标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17939172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:27:24  来源:igfitidea点击:

Add Checkbox to header of gridview

c#asp.net.netvb.netvisual-studio-2008

提问by C Sharper

I have following gridview:

我有以下网格视图:

enter image description here

在此处输入图片说明

I wanted to put checkbox to header of this grid, means below or besides Maths,Physics,Chemistry,Biology Header Text.

我想把复选框放在这个网格的标题中,意思是在数学、物理、化学、生物标题文本下方或之外。

Code for Grid:

网格代码:

<asp:GridView ID="GvSearch" runat="server" CellPadding ="3"
                                Width="100%" AutoGenerateColumns="False">
           <Columns>
               <asp:TemplateField>
                   <ItemTemplate>
                       <asp:Label ID="lblCity" runat="server"  Text='<%# Bind("City") %>' ></asp:Label>
                       <asp:CheckBox ID="ChkCity" runat="server" />
                   </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="Maths">

                   <ItemTemplate>
                       <asp:Label ID="lblMaths" runat="server" Text='<%# Bind("Maths") %>'></asp:Label>

                   </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="Physics">

                   <ItemTemplate>
                       <asp:Label ID="lblPhysics" runat="server" Text='<%# Bind("Physics") %>'></asp:Label>

                   </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="Chemistry">

                   <ItemTemplate>
                       <asp:Label ID="lblChemistry" runat="server" Text='<%# Bind("Chemistry") %>'></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="Biology">
                   <ItemTemplate>
                       <asp:Label ID="lblBio" runat="server" Text='<%# Bind("Biology") %>'></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
       </Columns>
         </asp:GridView>

I tried adding it as:

我尝试将其添加为:

<asp:TemplateField HeaderText="Physics">
                   <HeaderTemplate>
                   <asp:CheckBox ID="ChkCity" runat="server" />
                   </HeaderTemplate>
                   <ItemTemplate>
                       <asp:Label ID="lblPhysics" runat="server" Text='<%# Bind("Physics") %>'></asp:Label>

                   </ItemTemplate>

But this didnt worked.

但这没有用。

Please help me to add checkbox to header of gridview.

请帮我将复选框添加到 gridview 的标题。

回答by Tim

Not sure, but I think the HeaderTextproperty in TemplateFieldmight be causing problems with the HeaderTemplate. Looking at a code sample on MSDN, they don't use HeaderTextwhen using the HeaderTemplate. Try something like this (not tested):

不确定,但我认为HeaderTextin的属性TemplateField可能会导致HeaderTemplate. 查看MSDN上的代码示例,他们HeaderText在使用HeaderTemplate. 尝试这样的事情(未测试):

<asp:TemplateField>
    <HeaderTemplate>
        <asp:CheckBox ID="ChkCity" Text="Physics" runat="server" />
    </HeaderTemplate>
    <ItemTemplate>
        ....
    </ItemTemplate>
</asp:TemplateField>