Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping through GridView rows and Checking Checkbox Control

I currently have a GridView which displays data from a Student Table, here is my Grid and associated SQLDataSource;

                      <asp:GridView ID="GridView2" 
                        style="position:absolute; top: 232px; left: 311px;" 
                            AutoGenerateColumns="false" runat="server"
                        DataSourceID="SqlDataSource4">
                        <Columns>
                            <asp:TemplateField>
                            <ItemTemplate >
                            <asp:CheckBox runat="server" ID="AttendanceCheckBox" />
                            </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField>
                            <ItemTemplate>
                            <asp:Label ID="studentIDLabel" Text='<%# Eval("StudentID") %>' runat="server"></asp:Label>
                            </ItemTemplate>
                            </asp:TemplateField>                       
                            <asp:BoundField DataField="Name" HeaderText="Name" />
                        </Columns>
                     </asp:GridView>

                    <asp:SqlDataSource ID="SqlDataSource4" runat="server"
                        ConnectionString="<%$ ConnectionStrings:RegisterConnectionString %>" 
                        SelectCommand="SELECT [StudentID], [Name] FROM [Student] WHERE CourseID = @CourseID ">                         
                        <SelectParameters>
                            <asp:ControlParameter ControlID="CourseDropDownList" Name="CourseID" 
                                PropertyName="SelectedValue" Type="Int32" />
                        </SelectParameters>
                    </asp:SqlDataSource>

I have a button on the page which when the user clicks the button I need to loop through each row in the GridView, then find the CheckBox then I need to check if the checkbox is checked or not. If the checkbox is checked I need to add the value in the Label Template Field to a different table in the database. I am using C# Code. Any help is much appreciated, thanks in advance!

like image 369
user715115 Avatar asked Apr 21 '11 10:04

user715115


People also ask

How to use CheckBox in GridView?

From the GridView s smart tag, click on the Edit Templates link and then drag a CheckBox Web control from the Toolbox into the ItemTemplate . Set this CheckBox s ID property to ProductSelector . With the TemplateField and CheckBox Web control added, each row now includes a checkbox.


2 Answers

Loop like

foreach (GridViewRow row in grid.Rows)
{
   if (((CheckBox)row.FindControl("chkboxid")).Checked)
   {
    //read the label            
   }            
}
like image 135
V4Vendetta Avatar answered Sep 29 '22 12:09

V4Vendetta


you have to iterate gridview Rows

for (int count = 0; count < grd.Rows.Count; count++)
{
    if (((CheckBox)grd.Rows[count].FindControl("yourCheckboxID")).Checked)
    {     
      ((Label)grd.Rows[count].FindControl("labelID")).Text
    }
}
like image 38
Muhammad Akhtar Avatar answered Sep 29 '22 12:09

Muhammad Akhtar