By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 983 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

Walkthrough: Using a DataGrid Web Control to Read and Write Data

P: n/a
I follow the Walkthrough documented on Visual Studio to create an editable
data grid but it does not work on getting the value of the textbox in the
data grid. The code is copied here. I mark "*****" at which the categoryName
always get the past value no matter what it has been changed. How do I fix
it? Thanks.
Joe.
Private Sub DataGrid1_UpdateCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles
DataGrid1.UpdateCommand
Dim categoryName, categoryDescription As String

' Gets the value of the key field of the row being updated
Dim key As String = DataGrid1.DataKeys(e.Item.ItemIndex).ToString()

' Gets get the value of the controls (textboxes) that the user
' updated. The DataGrid columns are exposed as the Cells collection.
' Each cell has a collection of controls. In this case, there is only
one
' control in each cell -- a TextBox control. To get its value,
' you copy the TextBox to a local instance (which requires casting)
' and extract its Text property.
'
' The first column -- Cells(0) -- contains the Update and Cancel
buttons.
Dim tb As TextBox

' Gets the value the TextBox control in the third column
******
tb = CType(e.Item.Cells(2).Controls(0), TextBox)
categoryName = tb.Text

' Gets the value the TextBox control in the fourth column
tb = CType(e.Item.Cells(3).Controls(0), TextBox)
categoryDescription = tb.Text

' Finds the row in the dataset table that matches the
' one the user updated in the grid. This example uses a
' special Find method defined for the typed dataset, which
' returns a reference to the row.
Dim r As dsCategories.CategoriesRow
r = DsCategories1.Categories.FindByCategoryID(key)

' Updates the dataset table.
r.CategoryName = categoryName
r.Description = categoryDescription

' Calls a SQL statement to update the database from the dataset
SqlDataAdapter1.Update(DsCategories1)

' Takes the DataGrid row out of editing mode
DataGrid1.EditItemIndex = -1

' Refreshes the grid
DataGrid1.DataBind()
End Sub
Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Joe,

You are probably re-populating your datagrid on each page load and thus
resetting all the data before you are reading it.

Wherever you are populating the datagrid wrap that code in an if then so it
only gets populated on the intial page load like this:

If Not Page.IsPostBack Then
'---Populate your datagrid here
End If

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Joe Au" <jo***@livingfaith.org.hk> wrote in message
news:eP**************@TK2MSFTNGP12.phx.gbl...
I follow the Walkthrough documented on Visual Studio to create an editable
data grid but it does not work on getting the value of the textbox in the
data grid. The code is copied here. I mark "*****" at which the categoryName always get the past value no matter what it has been changed. How do I fix
it? Thanks.
Joe.
Private Sub DataGrid1_UpdateCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles
DataGrid1.UpdateCommand
Dim categoryName, categoryDescription As String

' Gets the value of the key field of the row being updated
Dim key As String = DataGrid1.DataKeys(e.Item.ItemIndex).ToString()

' Gets get the value of the controls (textboxes) that the user
' updated. The DataGrid columns are exposed as the Cells collection.
' Each cell has a collection of controls. In this case, there is only
one
' control in each cell -- a TextBox control. To get its value,
' you copy the TextBox to a local instance (which requires casting)
' and extract its Text property.
'
' The first column -- Cells(0) -- contains the Update and Cancel
buttons.
Dim tb As TextBox

' Gets the value the TextBox control in the third column
******
tb = CType(e.Item.Cells(2).Controls(0), TextBox)
categoryName = tb.Text

' Gets the value the TextBox control in the fourth column
tb = CType(e.Item.Cells(3).Controls(0), TextBox)
categoryDescription = tb.Text

' Finds the row in the dataset table that matches the
' one the user updated in the grid. This example uses a
' special Find method defined for the typed dataset, which
' returns a reference to the row.
Dim r As dsCategories.CategoriesRow
r = DsCategories1.Categories.FindByCategoryID(key)

' Updates the dataset table.
r.CategoryName = categoryName
r.Description = categoryDescription

' Calls a SQL statement to update the database from the dataset
SqlDataAdapter1.Update(DsCategories1)

' Takes the DataGrid row out of editing mode
DataGrid1.EditItemIndex = -1

' Refreshes the grid
DataGrid1.DataBind()
End Sub

Nov 18 '05 #2

P: n/a


thanks to S. Justin Gengo.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.