Filtering a dataset

  • Thread starter Thread starter Craig G
  • Start date Start date
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
 
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
///
 
Back
Top