If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ASP > ASP/VB.net Sort then Update

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-12-06, 16:43
pebkac pebkac is offline
Registered User
 
Join Date: Jan 2004
Posts: 35
Question ASP/VB.net Sort then Update

I am trying to be able to sort a Datagrid and then update allow the user be able to update it based on a selected index. Only problem is when I Sort it I am using a DataView, then when I update, I update the Dataset which potentially has a different row index. So Basically in my sorted view it could be row 7 but in my unsorted DataSet/Datatable it could be row 15. I do have a primary key defined, maybe I can utilize that instead of using the row index?

Hopefully I am making sense, I am fairly new to ASP.net. Any help you can give me would be great! I have posted some code below.



Private Sub dg_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEvent Args) Handles dg.SortCommand
Cache("strSort") = e.SortExpression().ToString
FillDG(cmd)
End Sub

Private Sub DGDataBind()
Dim dv As DataView
dv = New DataView(ds.Tables("Results"))
dv.Sort = Cache("strSort")
dg.DataSource = dv
dg.DataBind()
End Sub


Private Sub FillDG(ByVal cmd As SqlCommand)
'Getting Cached DS if exists, otherwise getting Data From DB
If Cache("CachedDS") Is Nothing Then
cmd.CommandType = CommandType.StoredProcedure
Dim da As New SqlDataAdapter(cmd)
da.Fill(ds, "Results")
Cache("CachedDS") = ds
Else
ds = Cache("CachedDS")
End If
DGDataBind()
End Sub



Private Sub dg_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles dg.UpdateCommand
Try
' GET DATASET
ds = Cache("CachedDS")


''====== GETTING UPDATED DATA FROM TEXT BOXES ========
Dim OldNum As TextBox = CType(e.Item.Cells(2).Controls(0), TextBox)
Dim NewNum As TextBox = CType(e.Item.Cells(3).Controls(0), TextBox)
Dim OldNumber As String = OldNum.Text
Dim New Number As String = NewNum.Text


''''====== CREATING PRIMARY KEY ========
''http://aspnet.4guysfromrolla.com/articles/101602-1.2.aspx
Dim pkey(1) As DataColumn
pkey(0) = ds.Tables("Results").Columns(1) ' Patcom DR
ds.Tables("Results").PrimaryKey = pkey


'http://www.sitepoint.com/article/asp-net-server-controls/25
' Gets current Row number
Dim row As Integer = CInt(e.Item.ItemIndex)

' UPDATING DATASET
ds.Tables("Results").Rows(row).Item(1) = OldNumber
ds.Tables("Results").Rows(row).Item(2) = NewNumber

' Setting datagrid source and Caching Dataset
dg.DataSource = ds.Tables("Results")
Cache("CachedDS") = ds

dg.EditItemIndex = -1
DGDataBind()

Catch ce As ConstraintException
dg.DataSource = ds.Tables("Results")
dg.EditItemIndex = -1
dg.DataBind()
lblError.Text = "DUPLICATE NUMBERS NOT ALLOWED"
Catch ex As Exception
dg.DataSource = ds.Tables("Results")
dg.EditItemIndex = -1
dg.DataBind()
lblError.Text = "UNKNOWN EXCEPTION HAS OCCURED"
End Try
End Sub
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On