CheckBoxField in GridView does not bind to string field in database

How do I bind a CheckBoxField in my GridView with a base db field which is a string. The string is either "1" or "0", but still the GridView will not readily bind to it. What am I doing. What is the best way to set the checkbox in the GridView and get it and set the row in the database (or underlying data source).

+1


source to share


3 answers


This should work:

Checked='<%# DataBinder.Eval(Container.DataItem, "MyStringField") = "1" %>'

      



Usually the checkbox value will map to the bit value in your database, so you won't get this problem.

+6


source


CheckBoxField is associated with a boolean. You can either convert the string to a boolean expression in the binding expression, or apply it in the db return.

It would make more sense for the database to store the state of the checkbox as a bit, not a string. Then this problem will disappear completely.



Of course, if you need to store the third gray state, it complicates things a bit, but you can still store the state as an int.

+1


source


I was working in VB and I tried something like this

<asp:checkbox runat="server" id="chkCastCool" enabled="false" 
     checked='<%CType(DataBinder.Eval(Container.DataItem,"Cast_Cool").ToString().Replace("Y","True").Replace("N","False"),Boolean)%>'/>

      

+1


source







All Articles