Sorting a dataview

  • Thread starter Thread starter Jason L James
  • Start date Start date
J

Jason L James

I have a defaultview of a data table
in a typed dataset. When I first populate
a tree view with the contents of the dataset
the records are in the correct sort order.

After adding a new record to the dataset, and
then refreshing the draw tree routine, the new
record is not in the correct sort order.

Is there anything that I should do to get the
sort order of the updated table/dataview to
refresh to include the new record?

Below is a small segment of code that sets the
defaultview sort property of the datatable in the
typed data set.

When I step through each of the rows in the
myComments() array the records are not in the
sort order specified.

Dim myCommentDR As DesignDataSet.CommentRow
myDesignDS.Comment.DefaultView.Sort = "cDate DESC"
Dim myComments() As DesignDataSet.CommentRow
myComments =
myProjectDR.GetChildRows("FK_Comment_Project")

For Each myCommentDR In myComments
........

Does anybody have any thgouhts on how I can
correct this logical error.

Thanks,

Jason.
 
In the sort property, just use the column name in quotes

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing
 
I thought that was what I had done, except that I want
them sorted in descending order. Yet it doesn't appear
to work with a newly added redcord.
 
Hi Jason

the full text is

Item ASC or Item DESC where ASC is default

I hopet this helps?

Cor
 
This is the text that I used:

myDesignDS.Comment.DefaultView.Sort = "cDate DESC"

and yet it still does appear to sort the records in the
dataview once a new record has been added.

Do you know of any reason why this might be the case?

Thanks,

Jason.
 
Hi Jason,

I never use the defaultview however always the dataview, and before I try it
can you try it yourself with that, something as

Dim dv as new dataview(myDesignDs.tables("Comment")
or your strongly typed variant which I also not use
dv.sort = "cDate DESC"
mydatagrid1.datasource = dv

And when it still is not working add the row using
dv.AddRow

And when than still not
datagrid1.refresh

And when than still not show than some code.

:-)

Cor
 
Cor,

my issue is actually not with the datagrid, but with
a chunk of code to represent the database on
a treeview control.

The datagrid stuff works fine. But since my treeview
must process child rows based on a relationship,
and getchildrows(rel_name) returns an array
of row objects, I think I need them sorted before
I execute the getchildrows command since once I
have run the command I have an array of
child rows and not a DV to sort. I only seem
to be able to sort a DV and yet the getchildrows
only works on a table in a dataset!

THis one's getting a bit tricky to solve.

Do you have any other ideas or pointers
that might be useful.

The code I am using to process the dataset and
load it into the treeview is below.

myDesignDS is my typed dataset object. DesignDataSet
is by typed class. It has three tables called Project,
Comment and Attachment. tvProjects is my TreeView.

Dim myProjectDR As DesignDataSet.ProjectRow
For Each myProjectDR In myDesignDS.Project.Rows
Dim ProjectNode As New TreeNode
ProjectNode.Text = myProjectDR.pProjectName

Dim myCommentDR As DesignDataSet.CommentRow
myDesignDS.Comment.DefaultView.Sort = "cDate DESC"
Dim myComments() As DesignDataSet.CommentRow
myComments =myProjectDR.GetChildRows("FK_Comment_Project")

For Each myCommentDR In myComments
Dim CommentNode As New TreeNode
CommentNode.Text =myCommentDR.dComment

Dim myAttach As DesignDataSet.AttachmentRow
Dim myAttachments() As DesignDataSet.AttachmentRow
myAttachments = myCommentDR.GetAttachmentRows
For Each myAttach In myAttachments
Dim myAttachNode As New TreeNode
myAttachNode.Text = myAttach.aName

CommentNode.Nodes.Add(myAttachNode)
Next
ProjectNode.Nodes.Add(CommentNode)
Next
tvProjects.Nodes.Add(ProjectNode)
Next

Once again, many thanks,

Jason.
 
Hi Jason,

I did send a little bit else because I had changed it this code yesterday to
somebody,

It sorts a dataset, that you should not use. It is the use of the
datarowview where I want to point you on, that part is not easy to find
in/on MSDN.

Because of your code of the threeview I saw, I assume that you can implement
the rest yourself.

(That dvr.Row can be as well dvr("mycolumn"))

\\\
Dim dv As New DataView(dt)
dv.Sort = "bla DESC"
Dim dtnew As DataTable = dt.Clone
For Each dvr As DataRowView In dv
dtnew.ImportRow(dvr.Row)
Next
dt.Clear()
dt = dtnew.Copy
///

I hope this helps?

Cor
 
Cor,

thanks for the help this morning. I ended up using a dataview
of the table, setting the sort order and then the row filter,
instead of getchildrows. Had to dump some of the
typed DS stuff to make it work, but all seems fine now.

Below is the code. I would appreciate any comments
on improving it if you have the time.

For Each myProjectDR In myDesignDS.Project.Rows
Dim ProjectNode As New TreeNode
ProjectNode.Text = myProjectDR.pProjectName
myDesignDS.Comment.DefaultView.Sort = "cDate DESC"
Dim myCommentDV As DataView =
myDesignDS.Comment.DefaultView
myCommentDV.RowFilter = "cpID = " & myProjectDR.pID
Dim myComments As DataRowView 'DesignDataSet.CommentRow
For Each myComments In myCommentDV 'myComments
Dim CommentNode As New TreeNode
CommentNode.Text =myComments(4)
myDesignDS.Attachment.DefaultView.Sort = "aName ASC"
Dim myAttachDV As DataView =
myDesignDS.Attachment.DefaultView
myAttachDV.RowFilter = "acID = " & myComments(0)
Dim myAttachment As DataRowView
For Each myAttachment In myAttachDV
Dim myAttachNode As New TreeNode
myAttachNode.Text = myAttachment(2)
CommentNode.Nodes.Add(myAttachNode)
Next
ProjectNode.Nodes.Add(CommentNode)
Next
tvProjects.Nodes.Add(ProjectNode)
Next

Thanks

Jason.
 
Hi Jason,

Congratulated, when it works, why changing, the only thing what probably can
when I see it well, is setting this out of the loop because it is dynamic.

myDesignDS.Attachment.DefaultView.Sort = "aName ASC"
Dim myAttachDV As DataView = myDesignDS.Attachment.DefaultView
myAttachDV.RowFilter = "acID = " & myComments(0)

However something I assume. so you can try and than it is even a peanut,
nobody will recognize.

Cor
 
Jason,
I would use a DataView for the outer loop, then use DataViewRow.GetChildView
to get the children for that row.

Something like:

For Each myProject As DataRowView In myDesignDS.Project.DefaultView
Dim ProjectNode As New
TreeNode(myProject("pProjectName").ToString())

' get the comments for the project
Dim viewComment As DataView =
myProject.CreateChildView("ProjectComments")
viewComment.Sort = "cDate DESC"

For Each myComments As DataRowView In viewComment
Dim CommentNode As New TreeNode(myComments(4).ToString())

' get the attachments for the comment
Dim viewAttachment As DataView =
myComments.CreateChildView("CommentAttachments")
viewAttachment.Sort = "aName ASC"

For Each myAttachment As DataRowView In viewAttachment
Dim myAttachNode As New
TreeNode(myAttachment(2).ToString())
CommentNode.Nodes.Add(myAttachNode)
Next
ProjectNode.Nodes.Add(CommentNode)
Next
tvProjects.Nodes.Add(ProjectNode)
Next

The advantage of the CreateChildView is you don't have to worry about
creating a well formed RowFilter for the children, especially if the
relationship involves text fields and/or multiple fields.

Hope this helps
Jay
 
Back
Top