How to set a checkbox in gridview using DataBinder.Eval

I am trying to check the box if the value is 1 or 0 mainly in my database. a field called Active (bit, not null), and I can pass the value to gridview .., but now I'm trying to check it if the bit is 1 or not checked, if the bit is 0, but it doesn't work .. it just shows unchecked, but the bit is 1.

   <ItemTemplate>
   <asp:CheckBox ID="ItemCheck" runat="server"
    Enabled='<%# (DataBinder.Eval(Container.DataItem, "Active")) %>' />
   </ItemTemplate>

Any help would be greatly appreciated

+5
source share
2 answers

Take a picture:

<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#Convert.ToBoolean(Eval("Active"))%>' .. />

Perhaps you can also do it like this:

<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#((bool)Eval("Active"))%>' .. />
+7
source

You can use CheckBoxFieldone that does this for you automatically and is the default subcontrol of the GridView

<asp:GridView ......>
  <Columns>
    <asp:CheckBoxField DataField="Active" SortExpression="Active" />
  </Columns>
</asp:GridView>

, RadioButtonList,

<asp:TemplateField ....>
    <ItemTemplate>
        <asp:RadioButtonList ID="rblActive" runat="server"
            SelectedValue='<%# Bind("Active") %>'
            RepeatDirection="Horizontal">
            <asp:ListItem Value="1">Enabled</asp:ListItem>
            <asp:ListItem Value="0">Disabled</asp:ListItem>
        </asp:RadioButtonList>
    <ItemTemplate>
</asp:TemplateField>
+1

All Articles