Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > TemplateColumn to display Checkbox in DataGrid

Reply
Thread Tools

TemplateColumn to display Checkbox in DataGrid

 
 
Paul
Guest
Posts: n/a
 
      04-20-2004
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.



 
Reply With Quote
 
 
 
 
Scott Mitchell [MVP]
Guest
Posts: n/a
 
      04-21-2004
> 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.velocityreviews.com/forums/(E-Mail Removed)
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Probelm with TemplateColumn and Checkbox ReskatoR ASP .Net 0 10-08-2008 02:38 PM
TemplateColumn with CheckBox DataBind in C# Qemajl ASP .Net Datagrid Control 2 02-28-2005 01:46 PM
TemplateColumn for this? conditionally display icon? djc ASP .Net Datagrid Control 2 11-08-2004 02:19 PM
Help with CheckBox inside a TemplateColumn JasmineC ASP .Net Datagrid Control 2 09-02-2004 09:26 AM
TemplateColumn CheckBox Nus Pieds ASP .Net 0 07-16-2003 02:16 AM



Advertisments