datagridview asp.net中的CheckBox [英] CheckBox in datagridview asp.net

查看:79
本文介绍了datagridview asp.net中的CheckBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库中有一个列,当我在dataGridView中显示它时,它显示为一个复选框,但是我不能点击这个复选框,我希望能够点击它并且从输出表中选择行来对它们做一些操作..任何帮助?





 SqlConnection con =  new  SqlConnection(ConfigurationManager.ConnectionStrings [  MyConnectionString ]的ConnectionString)。 

SqlDataAdapter adabt = new SqlDataAdapter();
DataTable table = new DataTable();
SqlCommand cmd = new SqlCommand( GetAllEmployees ,con); // 这将获取有关员工的所有数据
cmd.CommandType = CommandType.StoredProcedure;
adabt.Fill(table);
GridView1.DataSource = table;
GridView1.DataBind();

解决方案

如果直接将数据源绑定到定义绑定列的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 < span class =code-attribute> / >
< asp:boundfield datafield = PersonID headertext = PersonID >
InsertVisible =Fa lseReadOnly =TrueSortExpression =PersonID/>
< asp:boundfield datafield = 名称 headertext = 名称 >
SortExpression =名称/>
< asp:templatefield headertext = 选择 >
< itemtemplate < span class =code-keyword>>
< asp:checkbox id = chkSelect runat = server / >
& / itemtemplate >
< headertemplate >
< / headertemplate >
< / asp:templatefield >
< span class =code-keyword>< / asp:boundfield > < span class =code-keyword>< / asp:boundfield > < span class =code-keyword>< / columns >
< foot er style backcolor < span class =code-keyword> = #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 = 白色 / >
< < span class =code-leadattribute> alternatingrow style backcolor = 白色 / >
< ; / 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[^]


这篇关于datagridview asp.net中的CheckBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆