Thursday, March 29, 2012

Set checkbox in datagrid manually/through code

I have a numeric Oracle field which stores -1 for true and 0 for
false. I am trying to populate a checkbox in a datagrid. In my SQL
statement, I used the Decode function so that when it populates the
dataset that I would store the string 'true' and 'false' rather than a
numeric value. I did this because I thought it would make it easier
to actually set the value in the checkbox. Anyway, I bind the dataset
to the datagrid. In my HTML code, the checkbox is contained in a
templatecolumn:

<asp:CheckBox id="chkStrainerFuelLine" runat="server" Checked='<%#
(bool) DataBinder.Eval(Container.DataItem,
"strainer_on_fuel_line_bool") %>' Enabled="False"></asp:CheckBox
However, this will not work. It complains that "Specified cast is not
valid."

I have even tried it without the "(bool)" cast and it still didn't
work.

The data in my database has the value stored as either a -1 or 0. In
either case, I just want to set the check box to checked or unchecked.
I was even wondering if I needed to use one of the d atagrid's events
to do this.

Any ideas or help is appreciated.

Thanks,
MartySome suggestions

Try using a helper function to interpret your -1 or 0 instead of adjusting your recordset to return true or false

In your codebehind

protected bool GetCheckBoxSetting(object myObj

int myInt = Convert.ToInt16(myObj)
return (myInt == -1)

Then in your aspx

<asp:CheckBox id="chkStrainerFuelLine" runat="server" Checked='<%
GetCheckBoxSetting(DataBinder.Eval(Container.DataI tem, "strainer_on_fuel_line_bool")) %>' Enabled="False"></asp:CheckBox

If returning bool doesn't set the checkbox checked property correctly then try returning a string from GetCheckBoxSetting method

protected string GetCheckBoxSetting(object myObj

int myInt = Convert.ToInt16(myObj)
if (myInt == -1
return "true"
els
return "false"

HTH
Suresh

-- Marty wrote: --

I have a numeric Oracle field which stores -1 for true and 0 fo
false. I am trying to populate a checkbox in a datagrid. In my SQ
statement, I used the Decode function so that when it populates th
dataset that I would store the string 'true' and 'false' rather than
numeric value. I did this because I thought it would make it easie
to actually set the value in the checkbox. Anyway, I bind the datase
to the datagrid. In my HTML code, the checkbox is contained in
templatecolumn

<asp:CheckBox id="chkStrainerFuelLine" runat="server" Checked='<%
(bool) DataBinder.Eval(Container.DataItem
"strainer_on_fuel_line_bool") %>' Enabled="False"></asp:CheckBox

However, this will not work. It complains that "Specified cast is no
valid.

I have even tried it without the "(bool)" cast and it still didn'
work

The data in my database has the value stored as either a -1 or 0. I
either case, I just want to set the check box to checked or unchecked
I was even wondering if I needed to use one of the d atagrid's event
to do this

Any ideas or help is appreciated

Thanks,
Mart
It worked, thanks!

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

0 comments:

Post a Comment