TemplateColumn to display Checkbox in DataGrid

Discussion in 'ASP .Net Datagrid Control' started by Paul, Apr 20, 2004.

  1. Paul

    Paul Guest

    I am trying to display the contents of a numeric database column whose
    values are restricted to 0 or 1 as a checked or unchecked checkbox in my
    datagrid. I can display the database column's contents using an ordinary
    bound column so I know that I am successfully retrieving the data. The
    snippet from my aspx file where I try to display via a checkbox is:

    <asp:BoundColumn DataField="Location" HeaderText="Location">
    <HeaderStyle Width="100px"></HeaderStyle>
    </asp:BoundColumn>
    <asp:TemplateColumn HeaderText="Enabled">
    <HeaderStyle Width="25px"></HeaderStyle>
    <ItemTemplate>
    <asp:CheckBox runat="server" Checked='<%#
    DataBinder.Eval(Container, "DataItem.Enabled") %>'>
    </asp:CheckBox>
    </ItemTemplate>
    </asp:TemplateColumn>

    When the page is being generated, the first row of the grid shows the proper
    data in the "Location" bound column but an exception is thrown with the
    message:

    "Specified cast is invalid."

    when the "Enabled" column in the first row is being processed. I have tried
    things such as:

    <asp:CheckBox runat="server" Checked='<%# ((int)DataBinder.Eval(Container,
    "DataItem.Enabled"))==1 %>'>
    and
    <asp:CheckBox runat="server" Checked='<%# (((int)DataBinder.Eval(Container,
    "DataItem.Enabled"))==1)? true : false %>'>

    without any success.

    Any suggestions would be appreciated.
     
    Paul, Apr 20, 2004
    #1
    1. Advertising

  2. > I am trying to display the contents of a numeric database column whose
    > values are restricted to 0 or 1 as a checked or unchecked checkbox in my
    > datagrid. I can display the database column's contents using an ordinary
    > bound column so I know that I am successfully retrieving the data.


    Hi Paul. One option you might want to look into is Andy Smith's free
    BoundBooleanColumn control -
    http://metabuilders.com/Tools/BoundBooleanColumn.aspx. Much easier than
    having to muck around with TemplateColumns and databinding syntax! :)

    > I have tried things such as:
    >
    > <asp:CheckBox runat="server" Checked='<%# ((int)DataBinder.Eval(Container,
    > "DataItem.Enabled"))==1 %>'>
    > and
    > <asp:CheckBox runat="server" Checked='<%# (((int)DataBinder.Eval(Container,
    > "DataItem.Enabled"))==1)? true : false %>'>
    >
    > without any success.
    >
    > Any suggestions would be appreciated.


    Give the following a whirl:

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

    hth

    --

    Scott Mitchell

    http://www.4GuysFromRolla.com
    http://www.ASPFAQs.com
    http://www.ASPMessageboard.com

    * When you think ASP, think 4GuysFromRolla.com!
     
    Scott Mitchell [MVP], Apr 21, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nus Pieds

    TemplateColumn CheckBox

    Nus Pieds, Jul 16, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    394
    Nus Pieds
    Jul 16, 2003
  2. ReskatoR
    Replies:
    0
    Views:
    321
    ReskatoR
    Oct 8, 2008
  3. JasmineC

    Help with CheckBox inside a TemplateColumn

    JasmineC, Sep 2, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    132
    Eliyahu Goldin
    Sep 2, 2004
  4. djc

    TemplateColumn for this? conditionally display icon?

    djc, Nov 6, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    190
  5. Qemajl

    TemplateColumn with CheckBox DataBind in C#

    Qemajl, Feb 26, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    415
    Qemajl
    Feb 28, 2005
Loading...

Share This Page