Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Display Checkboxes of gridview checked or unchecked according to Database value...
i have 3 field in database paymentid(int),methodname(varchar50),Reimbursable(bit)


and i have gridview where id(label),methodname(label),Reimbursable(checkbox)

i bind data with gridview bt checkbox value is unchecked.unable to find error..


ASP.NET
<ItemTemplate>
    <asp:CheckBox ID="chkReimbursable" runat="server" 
       Checked='<%# Convert.ToBoolean(Eval("Reimbursable")) %>'/>
</ItemTemplate>


Thanx
Posted
Updated 8-Mar-12 20:32pm
v2
Comments
ProEnggSoft 9-Mar-12 2:32am    
Edit - Pre tag for ASP.NET code added - PES
Nilesh Patil Kolhapur 9-Mar-12 2:37am    
Your Code is ok check returned value

XML
<ItemTemplate>
    <asp:CheckBox ID="chkReimbursable" runat="server"
       Checked='<%# GetStatus(Eval("Reimbursable")) %>'/>
</ItemTemplate>



C#
protected bool GetStatus(string str)
   {
       if (str == "1")
           return true;
       else
           return false;
   }
 
Share this answer
 
Comments
Vani Kulkarni 26-Jun-12 2:54am    
My 5!
Manoj Kumar Choubey 26-Jun-12 3:01am    
My vote is +5
Try This :
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
       {

           int x = System.Convert.ToInt32(e.Row.RowIndex);
           CheckBox chk = (CheckBox)e.Row.FindControl("CheckBox1");
           // your select query from your database table
           // if it finds record from that table make chk.checked=true else false

       }
         
}



hope you understand and this will help you...
in Item Template :

XML
<asp:TemplateField HeaderText="Select Document">
 <ItemTemplate>
                <asp:checkbox id="CheckBox1" runat="server" />          
 </ItemTemplate>                                              
 
Share this answer
 
v3
<itemtemplate>
<asp:checkbox id="chkReimbursable" runat="server" enable="true" xmlns:asp="#unknown">
Checked='<%# GetStatus(Eval("Reimbursable")) %>'/>
 
Share this answer
 

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