Dataviews - How to return just one column

I Have a dataview which I wish to return just one column form the Customers table, I am completely stuck on this and any pointers would be gratefully recieved >

custDV = MyDataset.Tables("Customers").DefaultView

Returns the full table in the dataview and I would like to create another view from this with just the CustomerID column.

Thanks in advance

[412 byte] By [MarkJohn] at [2007-12-22]
# 1

Dim ColumnNames() As String = {"CustomerID"}

Dim MyTable as DataTable = DV.ToTable(True, ColumnNames)

Public Function ToTable(ByVal tableName As String, ByVal distinct As Boolean, ByVal ParamArray columnNames() As String) As System.Data.DataTable

Member of: System.Data.DataView

Summary:

Creates and returns a new System.Data.DataTable based on rows in an existing System.Data.DataView.

Parameters:

distinct: If true, the returned System.Data.DataTable contains rows that have distinct values for all its columns. The default value is false.

columnNames: A string array that contains a list of the column names to be included in the returned System.Data.DataTable. The DataTable contains the specified columns in the order they appear within this array.

tableName: The name of the returned System.Data.DataTable.

DMan1 at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

DMan1, Thank you for the reply. Unfortunately I keep getting the message" ToTable is not a member of System.Data.Dataview".

?

Thanks

MarkJohn at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3

The above noted was reference from the Object Browser in Visual Studio 2005 Visual Basic WinForm Application.

Public Class DataView

Inherits System.ComponentModel.MarshalByValueComponent

Member of: System.Data

Summary:

Represents a databindable, customized view of a System.Data.DataTable for sorting, filtering, searching, editing, and navigation.

VB05 .Net 2.0

DMan1 at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4

Showing a simple example of what Dmak was pointing you to. The ToTable method works fine.

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'//Create a datatable
Dim dt As New DataTable
dt.Columns.Add("col1")
dt.Columns.Add("col2")
dt.Rows.Add(1, 2)
dt.Rows.Add(3, 4)
dt.Rows.Add(5, 6)

'//Create a data view
Dim CustDV As DataView = dt.DefaultView

'//Only Copy col1 into a new datatabvle called MyTable
Dim COlumnNames() As String = {"col1"}
Dim MyTable As DataTable = CustDV.ToTable(True, COlumnNames)

'//Iterate through MyTable
For Each r In MyTable.Rows
MsgBox(r.item("col1"))
MsgBox(r.item(0))

'//These will fail as Only Created a single columns
'MsgBox(r.item("col2"))
'MsgBox(r.item(1))
Next
End Sub
End Class

spotty at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 5

Thanks guys for the responses, unfortunatel I am on 2003 with the earlier version of .net framework so I guess To.Table is out for me.

Thanks

MarkJohn at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...