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...
--Nath
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.Add("INTERNAL_ID")
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
OrderID"
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
one...
Try
If Not dsExtendedDataSet Is Nothing Then
If dsExtendedDataSet.Tables.Contains("ExtendedDataSet Data")
Then
If
dsExtendedDataSet.Tables("ExtendedDataSetData").De faultView Is
dgExtendedDataSets.DataSource Then
' check to make sure it isnt the first item in the
list
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 =
dsExtendedDataSet.Tables("ExtendedDataSetData")
dgExtendedDataSets.AllowSorting = True
' turn sorting off
dT.DefaultView.Sort = "OrderID"
' renumber our OrderID fields to reflect the move of the row...
RewriteExtendedDataSetOrderID(dT)
dgExtendedDataSets.DataSource = Nothing
dgExtendedDataSets.DataSource = dT.DefaultView
dT.DefaultView.Sort = "OrderID"
' refresh our data grid
dgExtendedDataSets.Refresh()
'dgExtendedDataSets.Update()
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
Else
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
Else
' 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") & ", " &
dT.Rows(A).Item("OrderID"))
' End If
'Next
End If
End If
Next
' finally re-order our view
dv.Sort = "OrderID"
End Sub