how to bind checkbox value from datatable in gridview
42,421
Solution 1
Put this code as your Item Template element:
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkIsSubscribed" runat="server" HeaderText="IsSubscribed"
Checked='<%#bool.Parse(Eval("IsSubscribed").ToString())%>' />
</ItemTemplate>
</asp:TemplateField>
Solution 2
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox
ID="chkIsSubscribed" runat="server" HeaderText="IsSubscribed"
Checked='<%#Convert.ToBoolean(Eval("IsSubscribed")) %>'/>
</ItemTemplate>
</asp:TemplateField>
please use this......
Solution 3
Eval() give an object type. So you have to use Eval(..).ToString() if you want to compare it... Like:
<asp:TemplateField HeaderText="Actif">
<ItemTemplate><asp:CheckBox ID="chkIsACTIF" runat="server" Enabled="false" Checked='<%# (Eval("ACTIF").ToString() == "1" ? true : false) %>' /></ItemTemplate>
<EditItemTemplate><asp:CheckBox ID="chkACTIF" runat="server" Checked='<%# (Eval("ACTIF").ToString() == "1" ? true : false) %>' Enabled="true" /></EditItemTemplate>
<FooterTemplate><asp:CheckBox ID="chkNewACTIF" runat="server" Checked="true" /></FooterTemplate>
</asp:TemplateField>
Author by
Admin
Updated on March 15, 2020Comments
-
Admin about 4 years
I need to figure out how to bind a CheckBox value in a GridView, I have written CheckBox.Checked= DataBinder.Eval(Container.DataItem, "IsSubscribed") in GridView, but the CheckBox is always checked, even when
IsSubscribed
is false.I have bound the grid in
Page_Load
, before the page has posted back. Here is my code:<asp:TemplateField HeaderText="Select"> <ItemTemplate> <asp:CheckBox ID="chkIsSubscribed" runat="server" HeaderText="IsSubscribed" Checked='<%# DataBinder.Eval(Container.DataItem, "IsSubscribed") %>'/> </ItemTemplate> </asp:TemplateField>
Thanks.
-
Simple Sandman about 9 yearsThis solution took care of the Format Exception:
String was not recognized as a valid Boolean