471,343 Members | 1,391 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,343 software developers and data experts.

datagrid dataset dataview datatable sorting

Hello Guys,

I have been trying to work this our for so long, but I just can't seem to
find the answer.

I am loading a datatable from a an access database using an
oledbdataadapter. I then assign the datatable.dataview to my
datagrid.datasource member so it will display my results..

I need to give the user an option to change the order of this data, I have a
field in my datagrid called OrderID, which is just basically an int column
which I loop through and set.

When the user selects a row in the data grid and then presses a button to
move that row up, I loop through the dataview and update the OrderID for
each row to reflect the changes. I then need to sort the dataview again by
OrderID. The problem with this is it isnt working.... I dont know why but
below is a copy of my code...

Any help would be great!! thanks so much guys

I hoipe the code makes sense.... if you want to simplify it i will...


Dim dsExtendedDataSet as DataSet

Sub LoadData

If Not dsExtendedDataSet.Tables.Contains("ExtendedDataSet Data") Then
dsExtendedDataSet.Tables.Add("ExtendedDataSetData" )

Dim ExTable As DataTable = dsExtendedDataSet.Tables("ExtendedDataSetData")
ExTable.Columns("INTERNAL_ID").AutoIncrement = True
ExTable.Columns("INTERNAL_ID").AutoIncrementSeed = 1

' setup primary key
Dim PrimaryKeyColumn(0) As DataColumn
PrimaryKeyColumn(0) = ExTable.Columns("INTERNAL_ID")
ExTable.PrimaryKey = PrimaryKeyColumn

Sql = "Select ID,ItemName,FieldType,ShowConditionOption,OptionSe t,OrderID
From ExtendedDataSetData Where ParentDataSetID=" & treeNode.Tag & " Order By
da = New OleDbDataAdapter(Sql, dbConn.db)
da.Fill(dsExtendedDataSet, "ExtendedDataSetData")

' make sure are field are properly setup
ExTable.Columns("ID").ReadOnly = True
ExTable.Columns("OrderID").AutoIncrement = True

ExTable.DefaultView.Sort = "OrderID"
dgExtendedDataSets.DataSource = ExTable.DefaultView

End Sub

Private Sub btnExMoveUp_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExMoveUp.Click
' we need to move the current select row of the extended data set up
If Not dsExtendedDataSet Is Nothing Then
If dsExtendedDataSet.Tables.Contains("ExtendedDataSet Data")
dsExtendedDataSet.Tables("ExtendedDataSetData").De faultView Is
dgExtendedDataSets.DataSource Then
' check to make sure it isnt the first item in the
If Not dgExtendedDataSets.CurrentRowIndex = 0 Then
' we can move this item up...
' we change the order id column value of this
item to the item just above it....
Dim dT As DataTable
dT =

dgExtendedDataSets.AllowSorting = True
' turn sorting off
dT.DefaultView.Sort = "OrderID"

' renumber our OrderID fields to reflect the move of the row...

dgExtendedDataSets.DataSource = Nothing
dgExtendedDataSets.DataSource = dT.DefaultView
dT.DefaultView.Sort = "OrderID"
' refresh our data grid

dgExtendedDataSets.AllowSorting = False
End If
End If
End If
End If
Catch ex As Exception
HandleError(ex, "trvInteriorDataSetSelect_BeforeSelect")
End Try
End Sub
Private Sub RewriteExtendedDataSetOrderID(ByRef dT As DataTable)

' this sub will loop through our extended data set view
' and rewrite the OrderID data for each record...
' this makes sure that everything is syncd and in order when we move
it back to the db

'NOTE: Can THROW exeption

Dim dv As DataView
dv = dT.DefaultView

' set the orderid field for each row...
' because there arnt many fields in the extendeddat sets data
' loop through the rows backward

Dim SetNextValueNewOrder As Boolean = False ' this variable will
tell our program to set a new order for this item and not just the standard
value of I
Dim I As Int16
Dim A As Int16
For I = dv.Count - 1 To 0 Step -1
If dgExtendedDataSets.CurrentRowIndex = I Then
' this is the item we are moving
dv.Item(I).Item("OrderID") = I - 1
SetNextValueNewOrder = True
If SetNextValueNewOrder Then
SetNextValueNewOrder = False
' this was the item before our row above before we moved
it up
' set this row to be one greater order id so the
previous row will be above it
dv.Item(I).Item("OrderID") = I + 1
' set this items Order to be equal to I
dv.Item(I).Item("OrderID") = I
'For A = 0 To dT.Rows.Count - 1
' If dv.Item(I).Item("INTERNAL_ID") =
dT.Rows(A).Item("INTERNAL_ID") Then
' MsgBox(dv.Item(I).Item("OrderID") & ", " &
' End If
End If
End If

' finally re-order our view
dv.Sort = "OrderID"
End Sub
Jan 19 '06 #1
0 1688

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by xrow | last post: by
4 posts views Thread by Steve B. | last post: by
7 posts views Thread by DC Gringo | last post: by
1 post views Thread by Jeremy | last post: by
2 posts views Thread by ddaniel | last post: by
1 post views Thread by troyblakely | last post: by
8 posts views Thread by Dennis | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.