Mark
Mark

Reputation: 53

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 from 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?

Upvotes: 0

Views: 1453

Answers (3)

Edgar
Edgar

Reputation: 11

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;

Upvotes: 1

Kelsey
Kelsey

Reputation: 47726

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"));
}

Upvotes: 0

MikeW
MikeW

Reputation: 5922

I belive you want something like this - this works for a DataGrid:

<asp:CheckBox ... 
   Checked='<%# Convert.ToBoolean( DataBinder.Eval(Container.DataItem, "is_checked"))%>'
/>

Upvotes: 0

Related Questions