Resorting a dataset

G

Guest

How can I re-sort a dataset.?
The code below doesn't do anything
Dim drs() As DataRow
Dim ds1 As DataSet, tab As DataTable
Dim dr As DataRow, dr1 As DataRow
Dim i As Integer, l As Integer
ds.Tables(0).DefaultView.Sort = "District, storeid desc"
drs = ds.Tables(0).Select("District=District")
ds1 = ds.Clone
tab = ds1.Tables(0)
l = tab.Columns.Count - 1
For Each dr In drs
dr1 = tab.NewRow
For i = 0 To l
dr1.Item(i) = dr.ItemArray(i)
Next
tab.Rows.Add(dr1)
Next
Return ds1.Tables(0).Rows
 
G

Guest

Sorting the dataview (which is only a view of the data) does not physically
sort the table rows. To see the outcome of using sort on the dataview try to
databind your dataview to a datagrid, e.g.

ds.Tables(0).DefaultView.Sort = "District desc"
datagrid1.datasource=ds.Tables(0).DefaultView
datagrid1.databind ()

then try

ds.Tables(0).DefaultView.Sort = "storeid desc"
datagrid1.datasource=ds.Tables(0).DefaultView
datagrid1.databind ()
 
G

Guest

Also if you wanted to create another table whose rows are sorted according to
the sort in the DataView then you need to use the datarowviews of the
dataview (instead of the rows of the table), e.g.

Dim drv As DataRowView
For Each drv In ds.Tables(0).DefaultView
dr1 = tab.NewRow
For i = 0 To l
dr1.Item(i) = drv(i)
Next

tab.Rows.Add(dr1)
Next
 

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

Similar Threads

Problem reading data into Dataset 6
Getting dataset? 5
Changing the Datatype of Datatable Column 3
dataset question 12
Dataset 0
DataViewHelp 1
Problem with a Gridview 3
Dataset Designer problems 0

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top