How do I assign an editable Checkbox in the datagrid

J

JLeary

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...
 
C

Cristian Suazo

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>
 
L

Lewis Wang [MSFT]

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" <[email protected]>
| 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: <[email protected]>
| 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...
|
|
|
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top