DataGrid TemplateColumns-- how-to use with CheckBoxes?

Discussion in 'ASP .Net' started by Jim Bancroft, Feb 7, 2005.

  1. Jim Bancroft

    Jim Bancroft Guest

    Hi everyone,

    I'd like to use ItemTemplates in my DataGrid to display one checkbox per
    row. The idea being, if field XYZ of a given DataItem is 1 (it's a binary
    field) then mark the checkbox. Otherwise, don't.

    Does this look like a good approach to the problem, or are there better ways
    that someone can share?
    .....
    <asp:TemplateColumn>
    <ItemTemplate>
    <asp:CheckBox runat="server" id="chkAddable"
    Checked=<%#DataBinder.Eval(Container.DataItem, "XYZ")/>
    </ItemTemplate>
    </asp:TemplateColumn>
    .....

    I'd like to cast that DataBinder.Eval() to a boolean, but I'm not sure how
    to do it. I've tried a few (bool) casts but I get syntax errors each time.
    Thanks for your advice and help.

    -Jim
     
    Jim Bancroft, Feb 7, 2005
    #1
    1. Advertisements

  2. Jim Bancroft

    SA Guest

    Jim:

    Maybe this is a copy/paste error, but you have a syntax problem in the
    snippet below: you don't have an ending %> for your Checked argument.

    I have always intercepted the ItemDataBound event for such things, but that
    was really more needed because the user could change the state of the
    checkbox (i.e. there was an edit item template too), not sure if that will
    be the case for you.

    Seems like it should work to me. Do cast to bool, because that will probably
    ouput the word "true", which is what you want of course.

    HTH,
     
    SA, Feb 8, 2005
    #2
    1. Advertisements

  3. Jim Bancroft

    Jim Bancroft Guest

    Thanks, Sven. I did make a copy/paste error. As to your suggestion, I'll
    keep plugging away.

    -Jim.
     
    Jim Bancroft, Feb 8, 2005
    #3
  4. Jim Bancroft

    SA Guest

    You could always add a protected method to your codebehind file like this

    Protected Function BitToBooleanString (ByVal bitValue As Integer) As String

    If (bitValue = 0) Then
    Return "False"
    Else
    Return "True"
    End If

    End Function

    You can then call the function from your aspx file

    <%# BitToBooleanString(DataBinder.Eval(Container.DataItem, "XYZ")) %>

    HTH
     
    SA, Feb 10, 2005
    #4
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.