且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

datagridview asp.net中的CheckBox

更新时间:2022-10-15 07:47:00

如果直接将数据源绑定到定义绑定列的gridview,则复选框将为显示为字段。

如果你想要复选框是可点击的,那么你需要定义模板列 CheckBoxField 用于BIT列。

请参阅下面的CheckBoxField链接示例:

aspnet-gridview-checkboxfield-example [ ^ ]



对于模板列,请参阅以下代码:

 <   asp:gridview     id   =  GridView1     runat   =  server    allowpaging   =  True  xmlns:asp   = #unknown >  
AllowSorting =TrueAutoGenerateColumns =FalseDataKeyNames =PersonID
DataSourceID =mySourceWidth =366pxCellPadding =4
ForeColor =#333333GridLines =None>
< >
< asp:commandfield showselectbutton = True / >
< asp:boundfield datafield = PersonID headertext = PersonID >
InsertVisible =Fa lseReadOnly =TrueSortExpression =PersonID/>
< asp:boundfield datafield = 名称 headertext = 名称 >
SortExpression =名称/>
< asp:templatefield headertext = 选择 >
< itemtemplate >
< asp:checkbox id = chkSelect runat = server / >
& / itemtemplate >
< headertemplate >
< / headertemplate >
< / asp:templatefield >
< / asp:boundfield > < / asp:boundfield > < / columns >
< foot er style backcolor = #990000 font-bold = True forecolor = 白色 / >
< row style backcolor = #FFFBD6 fo重新着色 = #333333 / >
< pager style backcolor = #FFCC66 forecolor = #333333 >
Horizo​​ntalAlign =Center/>
< selectedrow style backcolor = #FFCC66 font-bold = True forecolor = 海军 / >
< header style backcolor = #990000 font-bold = True forecolor = 白色 / >
< alternatingrow style backcolor = 白色 / >
&lt ; / pager style > < / asp:gridview >





另请参阅此链接:

选择GridView控件中的多个复选框 [ ^ ]


I have a column in my database which is bit column when i displayed it in the dataGridView it''s shown as a checkbox but i can''t click on this checkbox , i want to be able to click on it and select rows from the output table to do some action on them .. any help ?


SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);

SqlDataAdapter adabt = new SqlDataAdapter();
DataTable table = new DataTable();
SqlCommand cmd = new SqlCommand("GetAllEmployees", con); // this gets all data about employees
cmd.CommandType = CommandType.StoredProcedure;
adabt.Fill(table);
GridView1.DataSource = table;
GridView1.DataBind();

If you are directly binding datasource to a gridview defining bound columns then checkbox will be displayed for Bit fields.
If you want the checkbox to be clickable, then you need to define Template Column or CheckBoxField for BIT column.
See example in below link for CheckBoxField:
aspnet-gridview-checkboxfield-example[^]

For Template Column, refer following code:
<asp:gridview id="GridView1" runat="server" allowpaging="True" xmlns:asp="#unknown">
   AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="PersonID" 
   DataSourceID="mySource" Width="366px" CellPadding="4" 
   ForeColor="#333333" GridLines="None">
 <columns>
   <asp:commandfield showselectbutton="True" />
   <asp:boundfield datafield="PersonID" headertext="PersonID">
         InsertVisible="False" ReadOnly="True" SortExpression="PersonID" />
   <asp:boundfield datafield="Name" headertext="Name">
                                       SortExpression="Name" />
   <asp:templatefield headertext="Select">
    <itemtemplate>
       <asp:checkbox id="chkSelect" runat="server" />
    </itemtemplate>
    <headertemplate>
    </headertemplate>
   </asp:templatefield>
 </asp:boundfield></asp:boundfield></columns>
 <footerstyle backcolor="#990000" font-bold="True" forecolor="White" />
 <rowstyle backcolor="#FFFBD6" forecolor="#333333" />
 <pagerstyle backcolor="#FFCC66" forecolor="#333333">
                           HorizontalAlign="Center" />
 <selectedrowstyle backcolor="#FFCC66" font-bold="True" forecolor="Navy" />
 <headerstyle backcolor="#990000" font-bold="True" forecolor="White" />
 <alternatingrowstyle backcolor="White" />
</pagerstyle></asp:gridview>



Also refer this link:
Selecting multiple checkboxes inside a GridView control[^]