473,399 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 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...

--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
Jan 19 '06 #1
0 1807

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: DelphiBlue | last post by:
I have a Nested Datagrid that is using a data relations to tie the parent child datagrids together. All is working well with the display but I am having some issues trying to sort the child...
1
by: xrow | last post by:
Hello I have a simple webservice / c# application that receives data from server and prints the data in the asp:datagrid control I have problem when sorting data in datagrid I have created...
4
by: Steve B. | last post by:
I have a DataGrid on the left and TextBoxes (TB) on the right. The TB's reflect the contents of the grid cells. Sorting of columns (both thru VS and programmatically) work fine except, when the...
7
by: DC Gringo | last post by:
I have a datagrid that won't sort. The event handler is firing and return label text, just not the sort. Here's my Sub Page_Load and Sub DataGrid1_SortCommand: -------------------- Private...
1
by: Jeremy | last post by:
I want my gird to sort only the items on the current page when I click on a column header. I wrote a little test app, but when I sort it pulls in items from other pages and places them on the current...
2
by: ddaniel | last post by:
I have read many posts and seen many papers on the different techniques for sort and filtering datagrids. Many do re-queries against the dB ala Fritz Onion. I am trying to leverage the Dataview....
1
by: troyblakely | last post by:
I am having trouble sorting a datagrid. I have read numerous posts on this and other lists, and tried most of the suggestions, but none of them have worked for me yet. I populate a dataset from two...
8
by: Dennis | last post by:
How do I sort a DataGrid Column in code without clicking on the column header? -- Dennis in Houston
0
by: rupalirane07 | last post by:
Both grids displays fine. But the problem is only parent datagrid sorting works fine but when i clik on child datagrid for sorting it gives me error: NullReferenceException error Any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.