13

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!

Eric Barr
  • 3,999
  • 5
  • 29
  • 42
user715115
  • 159
  • 1
  • 3
  • 13

2 Answers2

42

Loop like

foreach (GridViewRow row in grid.Rows)
{
   if (((CheckBox)row.FindControl("chkboxid")).Checked)
   {
    //read the label            
   }            
}
V4Vendetta
  • 37,194
  • 9
  • 78
  • 82
  • See if you can find the control, then you can use all of it's properties, i.e. var checkbox = row.FindControl("chkboxid") as CheckBox; – Jeremy Jul 23 '15 at 18:38
  • I made a quick tweak, because I'm paranoid about null exceptions. foreach (GridViewRow row in grid.Rows) { var chk = (CheckBox)row.FindControl("chkboxid"); if (chk != null && chk.Checked) { //read the label } } – Laki Politis Jun 14 '17 at 14:00
  • I am always getting as checked as false only eventhough I checked the checkbox,I am using Vb.net – sarath Jul 02 '18 at 06:58
3

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
    }
}
Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
  • So this code will go through each row in GridView, find the checkbox control and Check to see if checkbox has been checked? How do I get the value from the Label template field to insert into another table in database? Thanks – user715115 Apr 21 '11 at 11:09
  • 1
    If the GridView has paging, the above answer will only loop through the records in the current page. – ssmsnet Apr 21 '15 at 12:55