Thursday, August 6, 2015

Populate Checkboxes of gridview checked or unchecked based on Database value



         Suppose you have a column in in your table as "SelectedFlag" as char datatype. And you are storing 'Y' or 'N' as per your requirement.  Now you have to modify your select statement like



"select  col1, col2, cast( (case when SelectedFlag='Y' then

                                 when SelectedFlag = 'N' then 0 end) as bit) as SelectedFlag from Table1"


 in GridView


  <asp:TemplateField HeaderText="Select">
     <ItemTemplate>
         <asp:CheckBox ID="ChkSelect" runat="server" Checked='<%# Eval("SelectedFlag") %>' />
     </ItemTemplate>
  </asp:TemplateField>

It will automatically check/uncheck your checkbox inside girdview based on your database.



No comments:

Post a Comment