Here we will see how to
filter and sort data in a dataset.
Data view holds the filtered
or required rows that are been retrieved based upon a particular condition. Here
we can get a customized view of a Data Table for Sorting and filtering data.
Program
Imports
System.Data
Imports
System.Data.OleDb
Public
Class Form1
Dim ConnectionString
As String =
System.Configuration.ConfigurationSettings.AppSettings("dsn")
Dim con As
OleDbConnection = New
OleDbConnection(ConnectionString)
Dim com As
OleDbCommand
Dim oledbda As
OleDbDataAdapter
Dim ds As
DataSet
Dim dt As
DataTable
Dim str As
String
Private Sub
btnfilterdata_Click(ByVal sender
As System.Object,
ByVal e As System.EventArgs)
Handles btnfilterdata.Click
Try
con.Open()
str = "select * from student"
com = New OleDbCommand(str, con)
oledbda = New
OleDbDataAdapter(com)
ds = New DataSet
oledbda.Fill(ds, "student")
Dim dv
As DataView = New DataView(ds.Tables("student"))
dv.RowFilter = "smarks>70"
dv.Sort = "smarks asc"
ListBox1.DataSource = dv
ListBox1.DisplayMember =
"smarks"
Catch ex As
Exception
MsgBox(ex.Message)
End Try
con.Close()
End Sub
End
Class
Output
Data table Select method
accepts a filter and sort argument to return an array.
Program
Imports
System.Data
Imports
System.Data.OleDb
Public
Class Form1
Dim ConnectionString
As String =
System.Configuration.ConfigurationSettings.AppSettings("dsn")
Dim con As
OleDbConnection = New
OleDbConnection(ConnectionString)
Dim com As
OleDbCommand
Dim oledbda As
OleDbDataAdapter
Dim ds As
DataSet
Dim dt As
DataTable
Dim str As
String
Private Sub
btnfilterdata_Click(ByVal sender
As System.Object,
ByVal e As System.EventArgs)
Handles btnfilterdata.Click
Try
con.Open()
str = "select * from student"
com = New OleDbCommand(str, con)
oledbda = New
OleDbDataAdapter(com)
ds = New DataSet
oledbda.Fill(ds, "student")
dt = ds.Tables("student")
Dim s1
As String
Dim s2
As String
s1 = "smarks>60"
s2 = "smarks asc"
Dim result()
As DataRow
result = dt.Select(s1, s2)
Dim ctr
As Integer
For ctr = 0
To (result.Length - 1)
ListBox1.Items.Add(result(ctr)("smarks"))
Next
Catch ex As
Exception
MsgBox(ex.Message)
End Try
con.Close()
End Sub
End
Class
Output