Click here to Skip to main content
15,891,745 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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 ?


C++
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();
Posted
Updated 12-Mar-13 18:38pm
v3
Comments
[no name] 12-Mar-13 21:03pm    
Please post your code that demonstrates this problem.
Hend Riad 12-Mar-13 21:07pm    
i'm not making any thing different in the code i just bind the data from DB, and as this column is saved as bit so it's transformed into checkbox .. but i didn't edit in anything else
Sergey Alexandrovich Kryukov 12-Mar-13 21:04pm    
System.Windows.Forms? Tag it.
Yes, you can click on it. If you think you cannot, you screwed up things very well. Without a short but comprehensive code sample, it's hard to say what.
—SA
Hend Riad 12-Mar-13 21:05pm    
but i'm not using windows forms, i'm dealing with web forms
Sergey Alexandrovich Kryukov 12-Mar-13 21:07pm    
I see "ASP.NET", good, but "datagridview" is confusing. Is there a class with such name in ASP.NET? I don't think so.
And think about the code sample I mentioned. No one will consider your problem is you don't show it. Use "Improve question".
—SA

1 solution

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.NET
<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[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900