Last post Jul 16, 2012 03:35 AM by Spytek
Jul 13, 2012 06:14 AM|Spytek|LINK
I have a sub that loops through a gridview and updates data in an SQL table based on a selected action from a dropdown list. The problem I am having is checking a row cell for a specific value. I want to check a cell from the 3rd column of my gridview and
if it matches a criteria then excute the rest of the statement.
I have attached my code for the sub and I need to know how I can assign the value of the cell to the sStatus variable as this is where I'm having problems.I hav tried Dim sStatus As String = row.Cells(2).Text but this is not working.
Any help will be appreciated. Thanks in advance.
Dim objCon As SqlConnection
Dim objCmd As SqlCommand
Dim strSQL As String
Dim ddl As DropDownList = gvDataList.HeaderRow.FindControl("ddlAction")
If ddl.SelectedValue <> "None" Then
' Iterate through the Products.Rows property
For Each row As GridViewRow In gvDataList.Rows
' Access the CheckBox
Dim cb As CheckBox = row.FindControl("chbxRowSelect")
Dim Row_ID As Integer = Convert.ToInt32(gvDataList.DataKeys(row.RowIndex).Value)
Dim sStatus As String = row.Cells(2).Text
If cb.Checked = True Then
If ddl.SelectedValue = "Archive" And sStatus = "failed sending to EDT Server" Then
strSQL = "update tbl*** set [Action]='" & ddl.SelectedValue & "' where [ID]=" & Row_ID
objCon = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("****"))
objCmd = New SqlCommand(strSQL, objCon)
objCmd.CommandType = CommandType.Text
Jul 13, 2012 07:26 AM|vinz|LINK
I hav tried Dim sStatus As String = row.Cells(2).Text but this is not working.
Are you using BoundField column or TemplateField column? If its a template then you'll have to use FindControl method to extract the value from the control.
Jul 13, 2012 07:52 AM|Mudasir.Khan|LINK
instead looping through all the rows try to update each row as and when the row is editing complete have a update button for each row
Jul 16, 2012 03:35 AM|Spytek|LINK
The only template column in the gridview is the checkbox column and the rest are bound columns including the 3rd column. Also there are no hidden columns. I used cells(2) as I understand that the first column is actually cell(0).
I know that if I ignore the cell comparison, the sub works and updates the table in the database but I need to check the contents of the 3rd column of the current row in the loop first. I'm using dotnetframework 3.5 and VS 2008.
Also the reason why i am using a loop is to make it easier to update all the selected checkboxes using a single click instead of having the user edit each row individually as there are many rows in the returned data.
Thanks for your help
Update: Just realised that the column is actually a template column so I will use the findcontrol method as mentioned. Thanks for your helo