help with asp.net gridview/checkboxfield binding
my stored procedure is returning either 1 or 0 depending on the value of another field, however, my checkboxfield in the gridview I've created to display the data returned fr开发者_C百科om the stored proc, is crashing saying that the value set to the checkboxfield is a string, not boolean. How can I take the field returned as 1 or 0 and convert it to boolean so my checkbox can bind to this value for checking/unchecking?
aspx
<asp:TemplateField SortExpression="TragamonedaActiva" HeaderText="Trag. Activa">
<ItemTemplate>
<asp:CheckBox ID="CK2" runat="server" EnableViewState="true"
Checked='<%# Convert.ToBoolean(Eval("TragamonedaActiva")) %>'/>
</ItemTemplate>
</asp:TemplateField>
.cs
isChecked = ((CheckBox)gvReport.Rows[rowNo].FindControl("CK1")).Checked;
I belive you want something like this - this works for a DataGrid:
<asp:CheckBox ...
Checked='<%# Convert.ToBoolean( DataBinder.Eval(Container.DataItem, "is_checked"))%>'
/>
Create a template field with your checkbox in the datagrid.
// In your aspx page
<asp:CheckBox ID="yourCheckBox" runat="server" OnDataBinding="yourCheckBox_DataBinding" />
// In your codebehind .cs file
protected void yourCheckBox_DataBinding(object sender, System.EventArgs e)
{
CheckBox chk = (CheckBox)(sender);
chk.Checked = Convert.ToBoolean(Eval("YourFieldName"));
}
精彩评论