Trying to re-sort a datatable

N

netloss

My program grabs a single datatable (dt) from an Excel file, loads it
into a dataset (ds) and displays it in a datagrid (dg).

The datatable has a couple of "calculated" columns that start out
blank. After loading the datattable, the program fills in these blank
columns using the values in other columns.

Next, I want to re-sort the datatable using the "calculated" columns. I
use the following code:


Ds.dt.DefaultView.Sort = "calculatedcol1, calculatedcol2"
Dg.DataSource() = ds.dt


This causes the datagrid to sort the datarows correctly. HOWEVER, I
also want to declare a loop that goes through each datarow in the
datatable and does something with that datarow. It's important that the
datarows are sorted as shown in the code. However, when I do the loop,
it's clear that the underlying datatable has not actually been
re-sorted.

My question is this: Is it possible either to re-sort the actual
underlying datatable, OR is it possible to somehow loop through each
value of the DefaultView instead of the table itself?

Thanks!!!!
 
V

Val Mazur \(MVP\)

Hi,

Sorting of the DataView does not provide actual physical sorting of the
underlying DataTable. What you need to do is to loop through the sorted
DataView using next kind of code

Dim loRow as DataViewRow
Dim loView as DataView

loView=dt.DefaultView
loView.Sort= .......

For each loRow in dt.loView
'do what you need here

Next
 
N

netloss

Thanks, but I encounter two problems when trying this:

1. There doesn't seem to be a dataViewRow type (I'm using VB.net if it
matters)

2. If I try to loop through the dataview with a standard datarow (or
alternately, a datarow derived from the dataset's class) I get a
system.invalidCastException
 
N

netloss

Thanks - but if I try to use a DataRowView object to iterate through my
DataView, it does not work. I'm trying something like this:

'''' Sort the datatable's defaultview according to my new parameters

dataset.datatable.defaultview.sort = "col1 DESC, col2 DESC"

'''' I now set a datagrid = to the newly sorted defaultview, to make
''' sure the sort worked correctly

datagrid1.datasource() = dataset.datatable

''' The datatable does indeed show the data in the sort order I just
''' specified (sorted by Col1 desc, Col2 desc).
''' Now I create another dataview and set it = to the default view
''' (just to be sure what I'm working with)

dim dvView as dataview = dataset.table.defaultview
dim drvRow as datarowview

''' I now iterate through the dataview with my datarowview object

for each drvRow in dvView
if drvRow.item(2) = "thisvalue" then...
next

This compiles, but it's using the *original* sort order of the
datatable, not the new sort order (Col1 desc, Col2 desc) that I tried
to specify in the dataview. Any suggestions?
 
V

Val Mazur \(MVP\)

Hi,

Try to apply same sorting to the newly created DataView and then iterate. It
should work
 
N

netloss

Thank you, that worked! (After I did as you suggested, I also realized
there was a problem with my code; when I fixed it, dvView no longer
needed to be re-sorted.)

I can now do what I originally wanted to do, and my remaining question
is just for learning. Thanks for putting up with me.

Question:: The dataset underlying this dataview is strongly typed, so I
had been using column names in my code when iterating through the
dataset. However, the dataview based on this dataset doesn't seem to be
strongly typed - or at least, I couldn't see any way of declaring a
customized datarowview object so I could refer to column names. So I
need to refer to columns by item number rather than actual name. Right?
Or is there some trick to making the dataview behave as if it were
strongly typed?

Thanks again!
 

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