Getting GridView CheckBox Value using VB.Net
The value of Checkbox control placed inside the ASP.Net GridView control can be accessed using the same method that we discussed in the previous tutorial of GridView FindControl Checkbox using VB.Net. The FindControl function returns the server control object as a naming container of server control. This same FindControl function can be used to access the server controls and their values placed inside the ASP.Net GridView Columns. The only difference is type casting of the associated control is required to access the relevant properties associated with any particular ASP.Net server control. For example Checked property value of checkbox that we will discuss in this tutorial. You can access the current state value of checkbox placed inside the GridView control using FindControl function. GridViewRow Class object enables to access the instance of a particular row and its Cells (Columns). You can access any particular cell by passing the integer value such as 0, 1 or 2 to access 1st, 2nd or 3rd column within the limit of number of columns in each row. If there are 3 columns in each row of GridView then you can access each cell by passing 0 for 1st column, 1 for 2nd column and 2 for 3rd column.
GridView Control Examples:
You can see the live samples and examples of GridView Control from the following links:
VB.Net Code to Access the Value of GridView Checkbox Item
Dim chk As CheckBox Label1.Text = String.Empty For Each rowItem As GridViewRow In GridView1.Rows chk = CType(rowItem.Cells(0).FindControl("chk1"), CheckBox) If chk.Checked Then Label1.Text &= GridView1.DataKeys(rowItem.RowIndex)("CategoryID").ToString() & "<br />" End If Next
Above VB.Net code shows how to find the checkbox control placed inside the ASP.Net GridView column and access its checked property value by type casting the control object returned by the FindControl function. You can also access the other database values for that particular row having checkbox checked state equal to true. In the above example code GridView DataKeys collection has been used to access the "categoryID" value representing the unique value for each GridView row items. If condition has been used here to test the value of checked property of checkbox in each row of GridView and shows the "categoryID" as output result on the web page for which it returns true.
Note: Categories table of SQL Northwind database has been used in the sample code to bind the data with GridView control.
You can refer the VB.Net Code for GridView CheckBox DataBinding tutorial to get the rest of the code required for this sample.
Continue to next tutorial: GridView CheckBox Select All using VB.Net to learn how to check all the checkbox controls rendered as the child controls of GridView control using VB.Net code.