Datagrid selected items with checkbox

B

Bnob

I read some articles about the way to find the rows having his checkbox
checked.

Here is the code

Private Sub btnModifier_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnModifier.Click
Dim dgItem As DataGridItem
Dim chkSelected As CheckBox

For Each dgItem In Me.DataGrid1.Items
chkSelected = dgItem.FindControl("chkSelect")
If chkSelected.Checked Then
'--- my code
End If

Next

end sub

In my asp page I have this for my datagrid:
<Columns>
<asp:TemplateColumn>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:CheckBox id="chkSelect" Runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>

My problem is when I click on my btnModifier button, my code never
execute. The if statment always return false.

Any idea?
 
K

Ken Cox [Microsoft MVP]

Hi,

Here's a sample that seems to do what you want. Perhaps you could try it and
let us know?

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If
End Sub

Private Sub btnModifier_Click _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnModifier.Click
Dim dgItem As DataGridItem
Dim chkSelected As CheckBox
Label2.Text = ""
For Each dgItem In Me.DataGrid1.Items
chkSelected = dgItem.FindControl("chkSelect")
If chkSelected.Checked Then
Label2.Text = Label2.Text & _
chkSelected.UniqueID & "<br>"
End If
Next
End Sub

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add _
(New 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



<asp:DataGrid id="DataGrid1" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="Boolean Value">
<ItemTemplate>
<asp:CheckBox id=chkSelect runat="server" Checked='<%#
DataBinder.Eval(Container, "DataItem.Boolean") %>' >
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Boolean Value">
<ItemTemplate>
<asp:Label id="Label1" runat="server">
<%# IIF(DataBinder.Eval(Container, "DataItem.Boolean"),"Yes","No")
%>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
<P>
<asp:Label id="Label2" runat="server"></asp:Label></P>
<P>
<asp:Button id="btnModifier" runat="server"
Text="Button"></asp:Button></P>

Does this help?

Ken
Microsoft MVP [ASP.NET]
Toronto
 
K

Ken Cox [Microsoft MVP]

Sorry, I guess I can't help you then because the code I provided works on my
system.


Bnob said:
Ken Cox [Microsoft MVP] avait écrit le 11.08.2004 :
chkSelected.Checked

Sorry
But the chkSelected.Checked statment is always FALSE!
 

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,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top