ARTICLE

Filtering a dataset using dataview and select method in VB.NET

Posted by Satyapriya Nayak Articles | Windows Forms VB.NET August 09, 2011
Here we will see how to filter and sort data in a dataset.
Reader Level:

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

ddv.gif

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

ddv1.gif

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor