Nick LaMarca
Nick LaMarca

Reputation: 8188

DataTable Sorting Then Assign Sorted Table Back To Original Table

I have a dataset that has a table called detail I want to sort. I know using a dataview I can sort the table with the following code..

Dim dvParsedDataset As New DataView(parsedDataset.Tables("Detail"))
            dvParsedDataset.Sort = AuthorizatonConstants.Auth_ID

I want to not only sort the table but assign it back to the original table in this case parsedDataset.Tables("Detail") but when assigning the sorted view back I get a read only error

parsedDataset.Tables("Detail") = dvParsedDataset.ToTable 'READ ONLY ERROR

How do I sort the table and also override the original table with the sorted table?

Upvotes: 0

Views: 198

Answers (2)

Lance Summers
Lance Summers

Reputation: 11

The Add does not work for me:

    dim RecTable as DataTabe
    Dim RecView as DataView
    Dim DT as DataTable

    RecTable = VideoDBDataSet.Tables("Recs")
    RecView = RecTable.DefaultView
    RecView.Sort = "Title ASC"
    Dt = RecView.ToTable
    VideoDBDataSet.Tables.Remove("Recs")
    VideoDBDataSet.Tables.Add(Dt)

VideoDBDataset.Recs.count now throws a NULL error But Videodbdataset.Tables("Recs").Rows.Count gives the correct number or records

So it looks like VideoDBDataset has lost the reference to Recs but still has the Tables("Recs")

Upvotes: 1

Steve
Steve

Reputation: 216313

You could remove the previous DataTable from the DataSet and the add the new table

DataTable detailCopy = dvParsedDataset.ToTable("Detail")
parsedDataset.Tables.Remove("Detail") 
parsedDataset.Tables.Add(detailCopy)

Upvotes: 2

Related Questions