arneweise77
Programmer
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
<aspataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False"
cellspacing="1"
cellpadding="2"
border="0"
alternatingitemstyle-backcolor="#e8ecf0"
itemstyle-backcolor="white"
itemstyle-horizontalalign="center"
Width="500"
Font-Name="Arial, Helvetica"
Font-Size="9pt"
MaintainViewState="false"
headerstyle-backcolor="blue"
headerstyle-forecolor="white"
headerstyle-font-size="10pt"
HeaderStyle-Font-Bold="True">
<Columns>
<asp:TemplateColumn HeaderStyle-HorizontalAlign=Center HeaderText="X">
<ItemTemplate>
<asp:Checkbox ID="chkSelection" Runat=server />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Vecka">
<ItemTemplate>
<asp:Label ID="simWeek"
Text='<%# DataBinder.Eval(Container.DataItem, "week" %>'
Runat=server />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Sparad simulering">
<ItemTemplate>
<asp:Label ID="saveInfo"
Text='<%# DataBinder.Eval(Container.DataItem, "save_info" %>'
Runat=server />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Notering">
<ItemTemplate>
<asp:Label ID="userInput"
Text='<%# DataBinder.Eval(Container.DataItem, "user_input" %>'
Runat=server />
</ItemTemplate>
</asp:TemplateColumn>
</columns>
</aspataGrid>
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
<aspataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False"
cellspacing="1"
cellpadding="2"
border="0"
alternatingitemstyle-backcolor="#e8ecf0"
itemstyle-backcolor="white"
itemstyle-horizontalalign="center"
Width="500"
Font-Name="Arial, Helvetica"
Font-Size="9pt"
MaintainViewState="false"
headerstyle-backcolor="blue"
headerstyle-forecolor="white"
headerstyle-font-size="10pt"
HeaderStyle-Font-Bold="True">
<Columns>
<asp:TemplateColumn HeaderStyle-HorizontalAlign=Center HeaderText="X">
<ItemTemplate>
<asp:Checkbox ID="chkSelection" Runat=server />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Vecka">
<ItemTemplate>
<asp:Label ID="simWeek"
Text='<%# DataBinder.Eval(Container.DataItem, "week" %>'
Runat=server />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Sparad simulering">
<ItemTemplate>
<asp:Label ID="saveInfo"
Text='<%# DataBinder.Eval(Container.DataItem, "save_info" %>'
Runat=server />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Notering">
<ItemTemplate>
<asp:Label ID="userInput"
Text='<%# DataBinder.Eval(Container.DataItem, "user_input" %>'
Runat=server />
</ItemTemplate>
</asp:TemplateColumn>
</columns>
</aspataGrid>