DataGrid TemplateColumns-- how-to use with CheckBoxes?

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
 
S

SA

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,
 
J

Jim Bancroft

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

-Jim.
 
S

SA

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top