Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
how to use checkboxes in grid view in asp.net with integer values not boolean

i have created checkboxes by using itemtemplate but it creates error on browser when i try to bind integer values to it using my sql table.

pls suggest something
Posted

1 solution

What error are you getting ? What is the Data Type that you are binding with that field ? If you it's a Boolean value and you want to bind it in CheckBox, there is no need, as GridView Automatically Takes care of all these. Boolean values renders in to CheckBox Only.
For Binding any other value, you can either use bind expression of that data field in aspx pages or in code Behind.
XML
<asp:GridView ID="GridView1" runat="server">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:checkbox id="CheckBoxid" runat="server" xmlns:asp="#unknown">
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView></asp:checkbox>


From the code behind, during RowDataBound, you can access it as below
C#
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
YourClass object = (YourClass)e.Row.DataItem;
CheckBox chkbox = e.Row.FindControl("CheckBoxid");
chkbox.Text = object.someProperties. 
}
}


Hope this will help !
 
Share this answer
 
Comments
Sachin gulati 28-Aug-11 22:28pm    
thank you :)

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