How do I assign an editable Checkbox in the datagrid

Discussion in 'ASP .Net Datagrid Control' started by JLeary, Jul 16, 2003.

  1. JLeary

    JLeary Guest

    Hi all,

    All I want to do is assign an editable checkbox to a column of type
    CHAR(1)..values 'Y' or 'N'. I have the template column specified, but I keep
    getting a cast exception. Is there no intrinsic conversion to 'bool' for
    this value range? I understand how to convert prior to update.

    <asp:TemplateColumn HeaderText="Billable">
    <ItemTemplate>
    <asp:CheckBox id="CheckBox1" runat="server" Checked='<%#
    DataBinder.Eval(Container,"DataItem.billable_flag") %>'></asp:CheckBox>
    </ItemTemplate>
    </asp:TemplateColumn>


    Thanks in advance...
     
    JLeary, Jul 16, 2003
    #1
    1. Advertising

  2. I suggest you user the ItemDataBound of the datagrid:

    ====================

    ALT 1.

    Protected Sub DataGrid1_ItemBound(ByVal sender As Object, ByVal e As
    DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
    If Not IsNothing(e.Item.DataItem) Then
    If Char.ToLower(CType(e.Item.DataItem("char_column"), Char)) = "y"
    Then
    Dim chk As CheckBox
    chk = e.Item.FindControl("chk")
    chk.Checked = True
    End If
    End If
    End Sub

    ====================
    ALT 2.
    If you still want to use the approach you tried earlier then you have to
    process your character within a function. For example:

    Code behind:

    Protected Function isChecked(ByVal yourChar As String)
    If yourChar.CompareTo("y") = 0 Then
    Return True
    Else
    Return False
    End If
    End Function


    ASPX page:

    <asp:DataGrid id=DataGrid1 runat="server">
    <Columns>
    <asp:TemplateColumn>
    <ItemTemplate>
    <asp:CheckBox ID="chk" Runat=server Checked='<%#
    isChecked(DataBinder.Eval(Container,"DataItem.char_column")) %>'/>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:DataGrid>





    "JLeary" <> wrote in message
    news:...
    > Hi all,
    >
    > All I want to do is assign an editable checkbox to a column of type
    > CHAR(1)..values 'Y' or 'N'. I have the template column specified, but I

    keep
    > getting a cast exception. Is there no intrinsic conversion to 'bool' for
    > this value range? I understand how to convert prior to update.
    >
    > <asp:TemplateColumn HeaderText="Billable">
    > <ItemTemplate>
    > <asp:CheckBox id="CheckBox1" runat="server"

    Checked='<%#
    > DataBinder.Eval(Container,"DataItem.billable_flag") %>'></asp:CheckBox>
    > </ItemTemplate>
    > </asp:TemplateColumn>
    >
    >
    > Thanks in advance...
    >
    >
     
    Cristian Suazo, Jul 16, 2003
    #2
    1. Advertising

  3. Hi JLeary,

    You can change code like this:
    <asp:TemplateColumn HeaderText="Billable">
    <ItemTemplate>
    <asp:CheckBox id="CheckBox1" runat="server"
    Checked='<%#
    Equals(DataBinder.Eval(Container,"DataItem.billable_flag"),(object)"Y")
    %>'></asp:CheckBox>
    </ItemTemplate>
    </asp:TemplateColumn>

    Or you can change the column of type from CHAR(1) to bit(1).

    HTH

    Best Regards,
    Lewis Wang
    Support Professional

    This posting is provided "AS IS" with no warranties, and confers no rights.

    --------------------
    | From: "JLeary" <>
    | Subject: How do I assign an editable Checkbox in the datagrid
    | Date: Wed, 16 Jul 2003 16:22:00 -0400
    | Lines: 18
    | X-Priority: 3
    | X-MSMail-Priority: Normal
    | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
    | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
    | Message-ID: <>
    | Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridcontrol
    | NNTP-Posting-Host: host-216-57-145-64.customer.veroxity.net 216.57.145.64
    | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
    | Xref: cpmsftngxa06.phx.gbl
    microsoft.public.dotnet.framework.aspnet.datagridcontrol:5715
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridcontrol
    |
    | Hi all,
    |
    | All I want to do is assign an editable checkbox to a column of type
    | CHAR(1)..values 'Y' or 'N'. I have the template column specified, but I
    keep
    | getting a cast exception. Is there no intrinsic conversion to 'bool' for
    | this value range? I understand how to convert prior to update.
    |
    | <asp:TemplateColumn HeaderText="Billable">
    | <ItemTemplate>
    | <asp:CheckBox id="CheckBox1" runat="server"
    Checked='<%#
    | DataBinder.Eval(Container,"DataItem.billable_flag") %>'></asp:CheckBox>
    | </ItemTemplate>
    | </asp:TemplateColumn>
    |
    |
    | Thanks in advance...
    |
    |
    |
     
    Lewis Wang [MSFT], Jul 18, 2003
    #3
    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. Alex
    Replies:
    0
    Views:
    459
  2. Replies:
    0
    Views:
    398
  3. Alex

    Including a Data Bound CheckBox in an Editable DataGrid...

    Alex, Oct 6, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    129
  4. Alex
    Replies:
    0
    Views:
    145
  5. Conditional checkbox in an editable datagrid

    , Oct 11, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    154
Loading...

Share This Page