Sorting dataset

A

Andy Baker

I have an invoicing application written in VB.NET 2003. The current invoice
is stored in a dataset, called DSInvoice, containing code, description,
quantity and price fields. It is bound to a datagrid to display it on
screen. I want to display the invoice lines on screen in the order in which
they are added, but when I print the invoice I want to print them in stock
code order. I am trying to use a DataView to do this, but cannot get it to
work. The code that I am using is:
Dim DVinvoice As DataView
DVInvoice = New DataView(DSInvoice.Tables("Details"))
DVInvoice.Sort = "Code ASC"
but this has no effect on the sort order of the DataView, it remains in the
same order as the original dataset. Is it something that I am doing wrong,
or does it need to be done differently with the Compact Framework? Thanks in
advance.

Andy Baker
 
W

W.G. Ryan [MVP]

Sorting the view won't have any effect on the dataset. However that code
should sort the view. Remember they are two totally different objects. How
have you verified that the view isn't getting sorted? If you're binding to
a grid or UI object, you may need to refresh the screen too
 
G

Guest

Hi Andy,

The CF implementation is the same as the full framework.

I created a DS and bound it to a Grid Control. I then used the exact lines
of code you posted to create a DV and bound it to a second Grid Control.
Both Grids were sorted correctly.

Without knowing details about the structure of your DS or how you are
printing the invoice, all I can offer is the following:

1) Is the code column a System.String but you are storing numeric values in
the column. if so, the numbers 1 to 10 would print out as 1, 10, 2, 3, 4
giving the impresion that the data is not being sorted.

2) What are you using to print the invoice? If it is the same Grid control
used for displaying the invoice (i.e. the one bound to the original DS), you
need to assign your DV as the DataSource of the Grid before you print. Just
creating the DV and not assigning it will not sort the data.

Chief Solutions Architect
Australian Special Information Systems (ASIS)
(MSysDev, MCT, MCPD: Win, MCITP: DBD, MCSD.NET, MCP: SQL Server 2000, MACS
PCP)
Blog: http://whartonj.blogspot.com
 
M

Milan

Hi Jeff,

Need small help
I have datatable which contain A1, Z1, D1, X1 and M1 column
want to sort them in asc like A1, D1, M1, X1 and Z1

Milan
 
A

Andy Baker

My print routine just prints line by line, so I was attempting to read
through the view using a for..next loop on DVInvoice.Table. Does this just
refer to the unsorted table? Is so, how can I read through the sorted view
line by line?
 
G

Guest

Using DVInvoice.Table actually access the rows contained in the underlying
table of the DataView, not the view. Therefore the rows will print according
to the sorting of the table.

What you need to use is:

For Each dr As DataRowView In DVinvoice

PrintRowCode......

Next

Which will return the rows of the sorted DataView instead of the underlying
DataTable

--
Chief Solutions Architect
Australian Special Information Systems (ASIS)
(MSysDev, MCT, MCPD: Win, MCITP: DBD, MCSD.NET, MCP: SQL Server 2000, MACS
PCP)
Blog: http://whartonj.blogspot.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top