Filtering a dataset

C

Craig G

at the moment i have a dataset that returns data

but i want to be able to filter the data so that there is only one occurence
of each item (i.e. DISTINCT)

the original dataset is populated by a stored_proc. i cant put distinct in
the stored_proc as it is a SQL2000 system_stored_proc

i was going to filter by using RowFilter method on a Dataview but cant use
DISTINCT

how can i do this?

Cheers,
Craig
 
C

Cor Ligthert

Hi Craig,

Before I knew there was a sample one on MSDN I made this one.

It uses the dataview and is therefore probably a little bit faster, you can
use it with option Strict On, it returns back whole rows of data and in my
opinion it is much simpler to use.

Maybe you can try it, it was just a try and I tried it now again, and it did
work?

Cor

\\\
Me.DataGrid1.DataSource = mydistinct.distinct(dt, "MyDistinctElement")
End Sub
Public Function distinct(ByVal dt As DataTable, _
ByVal dist As String) As DataTable
Dim dtclone As DataTable = dt.Clone
Dim dv As New DataView(dt)
dv.Sort = dist
Dim myselold As String = ""
For i As Integer = 0 To dv.Count - 1
If myselold <> dv(i)(dist).ToString Then
Dim drn As DataRow = dtclone.NewRow
For y As Integer = 0 To drn.ItemArray.Length - 1
drn(y) = dv(i)(y)
Next
myselold = dv(i)(dist).ToString
dtclone.Rows.Add(drn)
End If
Next
Return dtclone
End Function
///
 

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

getting subset of a Dataset? 3
Datatable = dataset.Tables("Supplier") ? 1
populate textbox from dataview 14
How to monitor dataset from dll 1
Dataset 2
Distinct groupid 1
Filling a Dataset 9
Calculation Time 2

Top