Datagrid with checkbox - how to select cell values?

K

KatB

Hi, here is my HTML for the datagrid:

<asp:datagrid id="dgToppings" style="Z-INDEX: 107; LEFT: 144px;
POSITION: absolute; TOP: 517px" tabIndex="4" runat="server"
Font-Names="Verdana" Font-Size="11pt" Height="40px" Width="336px"
GridLines="None" ShowHeader="False" BorderColor="Red"
AuogenerateColumns="False" CellSpacing="1"
CellPadding="0" BorderWidth="0px">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox ID="chkSelected" runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="ItemDesc">
</asp:BoundColumn>
<asp:BoundColumn DataField="ItemPrice"
DataFormatString="{0:C}"></asp:BoundColumn>
</Columns>
</asp:datagrid>

And here is my code (I'm trying to retrieve values in the row where the
chkSelected checkbox = true). On the

Dim dgItem As DataGridItem
Dim chkSelected As WebControls.CheckBox
Dim sTopping As String
Dim sToppingPrice As String

For Each dgItem In dgToppings.Items
Dim chkBox As CheckBox = CType(dgItem.FindControl("chkSelected"),
CheckBox)
If Not (chkBox Is Nothing) And chkBox.Checked Then
sTopping = CType(dgItem.FindControl("ItemDesc"), TextBox).Text
sToppingPrice = CType(dgItem.FindControl("ItemPrice"),
TextBox).Text
End If
Next

It find the checked box correctly but gets a "null reference" on
"sTopping = CType" line.

Clues anyone? I've tried several things I've seen out there...but no
luck.

Thanks, Kat
 
G

Guest

Hi Kat,

You're looking inside a bound column for something you'll never find. The
bound column is a table cell and has no ID or name. You need to reference its
objects by index rather than by name. An alternate would be to convert the
bound columns into template columns as you used with the chkbox. You could
insert label controls to hold the text.

Below, I've taken a stab at what I think you're trying to accomplish. Let us
know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto


<%@ Page Language="VB" Trace="true" %>
<script runat="server">

sub page_load
if not ispostback then
dgToppings.datasource=CreateDataSource()
dgToppings.databind()
end if

end sub



Sub Button1_Click(sender As Object, e As EventArgs)

Dim dgItem As System.Web.UI.WebControls.DataGridItem
Dim chkSelected As System.Web.UI.WebControls.CheckBox
Dim sTopping As String
Dim sToppingPrice As String
Dim chkBox As CheckBox
dim tc as System.Web.UI.WebControls.TableCell
For Each dgItem In dgToppings.Items
If dgItem.ItemType = ListItemType.Item Or _
dgItem.ItemType = ListItemType.AlternatingItem Then
chkBox = dgItem.FindControl("chkSelected")
If Not (chkBox Is Nothing)
if chkBox.Checked Then
tc=dgItem.cells(1)
if not isnothing(tc) then
sTopping=tc.text
response.write(sTopping)
end if
tc=dgItem.cells(2)
if not isnothing(tc) then
sToppingPrice=tc.text
response.write(sToppingPrice)
end if
end if
End If
end if
Next

End Sub

Function CreateDataSource() As system.data.DataTable
Dim dt As New system.data.DataTable
Dim dr As system.data.DataRow
dt.Columns.Add(New system.data.DataColumn _
("id", GetType(Int32)))
dt.Columns.Add(New system.data.DataColumn _
("itemdesc", GetType(String)))
dt.Columns.Add(New system.data.DataColumn _
("itemprice", GetType(Double)))
dt.Columns.Add(New system.data.DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
<asp:datagrid id="dgToppings" tabIndex="4" runat="server"
BorderWidth="0px" CellPadding="0" CellSpacing="1" AuogenerateColumns="False"
BorderColor="Red" ShowHeader="False" GridLines="None" Width="336px"
Height="40px" Font-Size="11pt" Font-Names="Verdana"
AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox ID="chkSelected" runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="ItemDesc"></asp:BoundColumn>
<asp:BoundColumn DataField="ItemPrice"
DataFormatString="{0:C}"></asp:BoundColumn>
</Columns>
</asp:datagrid>
</p>
<p>
<asp:Button id="Button1" onclick="Button1_Click" runat="server"
Text="Button"></asp:Button>
</p>
</form>
</body>
</html>
 

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