0

I have GridView named GridViewTest and the first column in the GridView is a templatefield checkbox named CheckBox1. Im able to loop through all the rows that the GridView contains and display the value in a label named Label2. The issue i'm having is, I only want the rows that are checked to be displayed in the Label2. Can anyone help me?

Protected Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click

   Dim str As String = ""
   For i As Integer = 0 To GridViewTest.Rows.Count - 1
       str = (str + GridViewTest.Rows(i).Cells(0).Text & Convert.ToString(" >> ")) + GridViewTest.Rows(i).Cells(1).Text + " >> " + >GridViewTest.Rows(i).Cells(2).Text + "<br/>"
   Next

   Label2.Text = str

End Sub

PS: This is VB ASP.NET

<asp:GridView ID="GridViewTest" runat="server" 
                       AutoGenerateColumns="False" BackColor="White" BorderColor="#DEDFDE" 
                       BorderStyle="None" BorderWidth="1px" CellPadding="4" 
                       CssClass="GridviewTable" DataSourceID="SqlDataSourceDetailGrid" EnableTheming="True" 
                       Font-Names="Arial" Font-Size="8pt" ForeColor="Black" GridLines="Vertical" 
                       Height="100%" PageSize="15" TabIndex="25" Width="985px" AllowPaging="True" EnableModelValidation="True">
                       <FooterStyle BackColor="#CCCC99" />
                       <HeaderStyle BackColor="#6B696B" CssClass="Freezing" Font-Bold="True" 
                           ForeColor="#FFFFCC" Wrap="False" />
                       <RowStyle BackColor="#F7F7DE" VerticalAlign="Middle" Wrap="False" />
                       <AlternatingRowStyle BackColor="White" />
                       <Columns>

                           <asp:TemplateField>
                               <EditItemTemplate>
                                   <asp:CheckBox ID="CheckBox1" runat="server" />
                               </EditItemTemplate>
                               <ItemTemplate>
                                   <asp:CheckBox ID="CheckBox1" runat="server" />
                               </ItemTemplate>
                           </asp:TemplateField>

                           <asp:BoundField DataField="TestColumn1" HeaderText="TestColumn1" />
                           <asp:BoundField DataField="TestColumn2" HeaderText="TestColumn2" />
                           <asp:BoundField DataField="TestColumn3" HeaderText="TestColumn3" />
                           <asp:BoundField DataField="TestColumn4" HeaderText="TestColumn4" />
                           <asp:BoundField DataField="TestColumn5" HeaderText="TestColumn5" />
                           <asp:BoundField DataField="TestColumn6" HeaderText="TestColumn6" />

                       </Columns>
                       <PagerSettings PageButtonCount="20" Position="TopAndBottom" />
                       <PagerStyle BackColor="#F7F7DE" Font-Size="10pt" ForeColor="Black" 
                           HorizontalAlign="Center" VerticalAlign="Middle" />
                       <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />

2
  • Can you post the markup of gridview? Commented Jan 14, 2016 at 22:56
  • Use the code I have mentioned in my answer. Just replace the id of check box with the id you are using. Commented Jan 14, 2016 at 23:36

1 Answer 1

1

If you are using a template field having a checkbox with an id of CheckBox1, then use the code below in the Page_PreRender event.

For Each row As GridViewRow In GridViewTest.Rows

   Dim result As Boolean = DirectCast(row.FindControl("CheckBox1"), CheckBox).Checked
   if result = True Then
      Label2.Text = string.Format("{0},row:{1} is {3}", Label2.Text , row.RowIndex, result);
   End If

Next
Sign up to request clarification or add additional context in comments.

1 Comment

Im getting an error: Index (zero bases) must be greater than or equal to zero and less than the size of the argument list. Please help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.