Monday, August 16, 2010

ASP.NET. VB.NET: How to access value of checkbox field in the gridview control.?

I have pulled data from SQL Server into a GridView control.


Two of the columns are checkbox fields.





During run-time,I am to click those columns to enter a check.


But how can I find out if the values of those items during run-tim





I need to allow the user to indicate whether they approve or deny a request; and then, I shall be able to update the corresponding columns in the originating database table.





What must I do to solve this problem?





During run-time, I added these two columns to the already existing ones:





[code]





Dim myCol1 As New DataColumn(';Approved';)


Dim myCol2 As New DataColumn(';Denied';)





myCol1.DataType = System.Type.GetType(';System.Boolean';)





myCol2.DataType = System.Type.GetType(';System.Boolean';)





myDataSet.Tables(0).Columns.Add(myCol1鈥?br>

myDataSet.Tables(0).Columns.Add(myCol2鈥?br>

myDataSet.Tables(0).AcceptChanges()


[/code]





I have code in myGridView_RowUpdating():





[code]





Try


'For Each column As GridViewRow In myGridView.Rows(myGridView.EditIndex)


' Access the CheckBox


'Dim cb As CheckBox = column.FindControl(';Approved';)





'Perhaps could also check to see if the index of the row equals myGridView.EditIndex


For Each row As GridViewRow In myGridView.Rows


' Access the CheckBox


Dim cb As CheckBox = row.FindControl(';Approved';)








If cb IsNot Nothing AndAlso cb.Checked Then


Console.WriteLine(';Found';)





'Here, I want to see if the Approved checkbox is checked.





End If


Next


Catch ex As Exception


Console.WriteLine(ex.Message)


End Try[/code]








This is the code that I placed in myGridView_RowCommand():





[code]





Static intCount As Integer


Dim intRowIndex As Integer








'If intCount = 0 Then


intRowIndex = CInt(e.CommandArgument.ToString())


'dim intColumnIndex as Integer = cint(Request.Form([';__EVENTARGUMENT';])





myGridView.EditIndex = intRowIndex 'Place this particular row of the GridView control in edit mode.





intCount += 1


'End If 'If intCount = 0





Dim intResponse As Integer


Dim strAnswered As String








If (e.CommandName.Equals(';Edit';)) Then





ElseIf (e.CommandName.Equals(';Update';)) Then


Console.WriteLine(';Update';)





' If myGridView.Rows(intRowIndex).Cells.Item(鈥?And myGridView.Rows(intRowIndex).Cells.Item(鈥?Then





' End If





'intResponse = MsgBox(';Are you sure that you want to approve this request?';, MsgBoxStyle.YesNoCancel, ';Confirmation Required';)


ElseIf (e.CommandName.Equals(';Cancel';)) Then





Else


'Nada


End If 'If (e.CommandName.Equals(';Edit';))





If intResponse = vbCancel Then


MsgBox(';The record has not been changed.';, MsgBoxStyle.OkOnly, ';No Action Taken';)








Exit Sub


End If





If intResponse = vbYes Then


'Retreive the value of the key





Dim strKey As String








'strKey = myGridView.DataKeys(intRowIndex).Values(鈥? 'Oops! There has been nothing assigned to DataKeys.





'Note: myGridView.Rows(intRowIndex).Cells.Item(鈥?is ';';, since it is the column of DELETE buttons.





'This gives the contents of the first row, first column. It would have been Cells.Item(0), had it not been


' displaced by the row of EDIT buttons.


strKey = myGridView.Rows(intRowIndex).Cells.Item(鈥?br>




'Call the handling routine, with strKey as a parameter.


'SupervisorResponse(strKey)


End If 'If intResponse = vbYes


[/code]ASP.NET. VB.NET: How to access value of checkbox field in the gridview control.?
from code I have doing the same type of thing but going after a textbox





Dim item as DataGridItem


foreach item in DataGridCart.Items


cartInfo = cartController.Get(CInt(DataGridCart.Dat鈥?br>

cartInfo.Quantity = Convert.ToInt32((directcast(item.Cells(3鈥?br>

next





The code was converted to VB for you from CSharp on the fly...but that's the basic idea.ASP.NET. VB.NET: How to access value of checkbox field in the gridview control.?
http://it.maconstate.edu/tutorials/

No comments:

Post a Comment