DataGrid TemplateColumns-- how-to use with CheckBoxes?

  • Thread starter Thread starter Jim Bancroft
  • Start date Start date
J

Jim Bancroft

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:

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,
 
Thanks, Sven. I did make a copy/paste error. As to your suggestion, I'll
keep plugging away.

-Jim.
 
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
 
Back
Top