Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checkbox in datagrid problem

Status
Not open for further replies.

arneweise77

Programmer
Apr 25, 2002
46
SE
Hi! I created a datagrid with some columns, the first column is populated with checkboxes. So far so good. But when a want to check which ones are checked, they all are false even if they are checked. Here´s the code:

Private Sub DeleteButton_Click( Sender As Object, E As EventArgs )
Dim oConnection As OleDbConnection
Dim oCommand As OleDbCommand
Dim strSQLQuery As String
Dim saveInfo As String
Dim myDataGridItem As DataGridItem
Dim chkSelected As System.Web.UI.WebControls.CheckBox

oConnection = New OleDbConnection(sConnString)
oConnection.open
For Each myDataGridItem In DataGrid1.Items
chkSelected = myDataGridItem.FindControl("chkSelection")
If chkselected.checked Then
saveInfo = CType(myDataGridItem.FindControl("saveInfo"), Label).Text
strSQLQuery = "Delete * FROM simulation" & _
" WHERE save_info = '" & saveInfo & "'"

oCommand = New OledbCommand(strSQLQuery, oConnection)
oCommand.ExecuteNonQuery()
End if
Next
oConnection.Close
Page_Load(Sender, E)

End Sub

<asp:DataGrid id=&quot;DataGrid1&quot; runat=&quot;server&quot; AutoGenerateColumns=&quot;False&quot;
cellspacing=&quot;1&quot;
cellpadding=&quot;2&quot;
border=&quot;0&quot;
alternatingitemstyle-backcolor=&quot;#e8ecf0&quot;
itemstyle-backcolor=&quot;white&quot;
itemstyle-horizontalalign=&quot;center&quot;
Width=&quot;500&quot;
Font-Name=&quot;Arial, Helvetica&quot;
Font-Size=&quot;9pt&quot;
MaintainViewState=&quot;false&quot;
headerstyle-backcolor=&quot;blue&quot;
headerstyle-forecolor=&quot;white&quot;
headerstyle-font-size=&quot;10pt&quot;
HeaderStyle-Font-Bold=&quot;True&quot;>
<Columns>
<asp:TemplateColumn HeaderStyle-HorizontalAlign=Center HeaderText=&quot;X&quot;>
<ItemTemplate>
<asp:Checkbox ID=&quot;chkSelection&quot; Runat=server />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText=&quot;Vecka&quot;>
<ItemTemplate>
<asp:Label ID=&quot;simWeek&quot;
Text='<%# DataBinder.Eval(Container.DataItem, &quot;week&quot;) %>'
Runat=server />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText=&quot;Sparad simulering&quot;>
<ItemTemplate>
<asp:Label ID=&quot;saveInfo&quot;
Text='<%# DataBinder.Eval(Container.DataItem, &quot;save_info&quot;) %>'
Runat=server />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText=&quot;Notering&quot;>
<ItemTemplate>
<asp:Label ID=&quot;userInput&quot;
Text='<%# DataBinder.Eval(Container.DataItem, &quot;user_input&quot;) %>'
Runat=server />
</ItemTemplate>
</asp:TemplateColumn>
</columns>
</asp:DataGrid>
 
Why do you use a for each statement when the FindControl Method runs for all the items directly from your datagrid object? plus you might want to cast your control:

[in c#]
chkSelected = (Checkbox) DataGrid1.FindControl(&quot;chkSelection&quot;);


 
Well, I´m not quite with you on the for each statement... I have to loop threw eah row to check if the checkbox is marked, rigth? The alternative could be 'DataGrid1.Item(i).FindControl(&quot;chkSelection&quot;´)'. But then i´ve to loop with i instead = roughly the same thing. I´ve tried without the for each statement and to cast with the CType but it doesn´t help, unfortunely.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top