Ron Hall

I am using VB.net 2003. I am creating a basic search engine for articles in our company libarary. I create a DataTable with the data pulled back from the SQL Server. I then create a DataView to sort the data into the order I want. Because we want to show only 20 items per page, I want to create a new DataTable in the order of the DataView and delete all but the first 20 records and go to the display. Since there is no DataView.ToTable in 2003, how do I get a display DataTable properly sorted




Re: Visual Basic General Copying From DataView to Datatable in VB 2003

Riquel Dong – MSFT

Hi,

You need to create new datatable with ordered rows from datagrid. Here is simple example code. Hope this can help you.

Code Snippet

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim conn As SqlConnection = New SqlConnection("Data Source=servername;Initial

Catalog=Northwind;Integrated Security=True")

Dim ds As DataSet = New DataSet()

Dim sel As String = "SELECT ShipperID, CompanyName FROM Shippers"

Dim da As SqlDataAdapter = New SqlDataAdapter(sel, conn)

da.Fill(ds, "Shippers")

Dim dv As DataView = New DataView(ds.Tables("Shippers"))

dv.Sort = "CompanyName DESC"

Me.DataGrid1.DataSource = dv

Dim dt As DataTable = New DataTable()

Dim myColumn As DataColumn

myColumn = New DataColumn()

myColumn.DataType = System.Type.GetType("System.Int32")

myColumn.ColumnName = "ShipperID"

dt.Columns.Add(myColumn)

myColumn = New DataColumn()

myColumn.DataType = Type.GetType("System.String")

myColumn.ColumnName = "CompanyName"

dt.Columns.Add(myColumn)

Dim dr As DataRow

For i As Integer = 0 To 2

dr = dt.NewRow()

For j As Integer = 0 To dv.Table.Columns.Count - 1

dr.Item(j) = Me.DataGrid1(i, j)

Next

dt.Rows.Add(dr)

Next

Me.DataGrid1.DataSource = Nothing

Me.DataGrid1.DataSource = dt

End Sub

Thanks for your question