Exforsys.com
 
Home Tutorials VB.NET 2005
 

Finding and Sorting Data in DataSets

 

Finding and Sorting Data in DataSets

In this tutorial you will learn about Finding and Sorting Data in DataSets - Filtering on Row State and Version, Sorting and Data View Manager.


Finding and Sorting Data in DataSets

Using the table's Select method or the RowFilter property of a data view, the user can filter records in a data table to make available only the required records. This is useful when the user wants to work with different subsets of the records in a dataset table. To specify filter criteria, the user can use the same expression syntax used to create column expressions. The filter expression is evaluated as a Boolean expression; if the expression returns true, the record is included. A filter expression might look like the following:


Price > 10.00


Filtering on Row State and Version

A dataset can maintain different versions of records in tables. When records are first filled in, the dataset contains the original version of the record. If record is changed, the dataset maintains a different version — the current version — that reflects the changes. A property on the record indicates whether the record is unmodified, updated, new, or deleted


A common use for filters is to specify only the current versions of records in the data table. If records have been changed, there are two versions of a record--the current version reflecting the change and the original version representing the record before any changes were made. Records are also flagged according to their status: new, unchanged, deleted, or modified. A deleted record, for example, still exists in the data table, but its row-state flag has been set to deleted.


Sorting

Sorting is similar to filtering, in that you specify a sort expression. A typical sort expression is simply the name of the column to sort by. For example, to sort by the OrderDate column, the user msut specify the sort expression OrderDate. However, the user can sort by the value of any expression, including calculated values. If table's Select method is called, the sort expression is passed as a parameter. If DataViews are being used, the sort expression is to be specified as the value of the view's Sort property.


Data View Manager

Individual DataViews can be defined to sort or filter the data in each DataSet table. If the DataSet contains multiple tables, an alternative is to create a DataView manager (a DataViewManager object). The DataView manager works something like a dataset-wide data view. It provides a single object that manages a collection of DataViews, each of which is associated with a specific table in a dataset. To use a DataView manager, the user must create and configure it in code. There is no design-time object that can be added to a form or component. This means that controls are bound to the DataView manager in code, as there is no design-time component to bind them to.


The following project will illustrate the filtering and sorting of the data.


1. Create a new Windows application project.
2. Add three labels, one DataGridView, one ComboBox and a button to the Form
3. And arrange them as shown in the screenshot below
4. Now go to the codes page and add the following codes.


Click here to veiw sample code


1.2.3.4.5.6.7.8.9.10.The DataGridView before Sorting:



The DataGridView after sorting on Name:



In the same way table data can be filtered. In this case all the instructions given for the above case holds good. The only change is in the EventHandler for the ComboBox. Instead of writing code for sorting, the activity for filtering will be defined. The Additional lines of code for this demo is given below:


Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged


Dim filtStr As String


filtStr = Me.ComboBox2.Text


Label3.Text = "The Data is Filtered for all Names starting with alphabet " & filtStr


dv.RowFilter = "Name like '" & filtStr & "*'"


End Sub


Now press F5 to execute the program. The screenshot given below shows the initial Screen.



The Screenshot Below shows the Screen With filtered data:




Read Next: Data Manipulation with ADO.NET



 

 

Comments


wjgreensr said:

  This page shows nice data but where is the tutorial and database so the user can try the example for themselves. I want to be able to this exact sorting and filter on my database - but I had no success. Please help
October 20, 2005, 3:39 pm

Piwe said:

  I would like to how you access Data from Database and make it appear on the datagrid using VB.NET.

Can some 1 help me please.
Thanx
July 19, 2006, 4:01 am

Lexus said:

  I\'m trying to do this with an Access Database. How can i build this tutorial to work with .mdb files? I can not found a good tutorial with filtering an Access Database.
August 21, 2006, 7:06 am

Hans said:

  The sorting works fine on numbers and characters. But when I tried to sort dates, it will only look at the first number.

Is there some way to sort a DateTime?
November 24, 2006, 5:58 am

Nader said:

  Would some body help me in this?
I want to connect to oracle database and do filter and display result using visual manner without writing any code.
And if some body provides me with the tutorial I will appreciate his assistant

Regards
November 28, 2006, 1:08 am

Esteban said:

  This page was really useful, thanks a lot
February 5, 2007, 10:05 am

Joyce said:

  Thanks for this nice example - but is this better than just clicking on the individual headers to sort? Also once sorted - how do you select a row - what I am doing is selecting a row and then filling another tab with that data - I can do it without sorting by using hittest - but after sorting the hittest method gives me the wrong row.
March 14, 2007, 9:59 am

thoudhry said:

  really i can learn the datagrid
March 21, 2007, 6:01 am

Anandkumar J said:

  HI,
The example is good but i want sort the record within dataset but i don't want to use dataview for that.
April 18, 2007, 2:32 am

Tida said:

  This example is really helpful, but I want to know if I would select distinct(name) from view that I created??
June 18, 2007, 9:31 pm

Mary said:

  This Web was useful very much
June 23, 2007, 5:03 am

sarav khan said:

  its good but i waana know about adobc
July 4, 2007, 8:41 am

Sami Khan said:

  it is Good but v want datacombo witin Datagrid . is it possible. when v Double Click on any Record in DV then the Record Should be Open in Form Data Entry Mode . so we able to Update it. if it is then Email me : sami_momand@yahoo.com , thanks
August 25, 2007, 2:14 am

Landford Heartily said:

  Thanks for the example above. It gives me "Inspiration"......Now I can implement it with my programs.
September 6, 2007, 10:41 pm

Inscore said:

  Thanks, this really helps understanding the dataset and dataview concept.
September 18, 2007, 4:58 am

ruksh said:

  thans, this example was really helpful
September 27, 2007, 8:35 am

Sukh said:

  Hi, Its really very good, but if u have add the coding of how to populate combo box with records that would be greate for the beginners just like me.
December 12, 2007, 10:42 pm

Ritika said:

  Hi, I am trying to write a filterexpresion so that I can extract first 40000 records from my datatable and then next 40000 in teh next loop.
The string filter I am using is DataRow[] rows = dtExport.Select("ColId < 40000 " i);
but this returns rows of length 0
Kindly let me the correct filter expression in this situation.
December 27, 2007, 9:17 am

vijay kumar dubey said:

  wow,its vvvvvvvvery helpful for me
thanks a lot
February 7, 2008, 12:41 am

Ashutosh Bagul said:

  this web site solve my all problems its a very good web site
August 6, 2008, 9:58 pm

Baljeet Bhardawj said:

  Dim dv As New DataView
dv = ds1.Tables(0).DefaultView
dv.Sort = ("room_re")
it sort only the view cannot the dataset
i want to sort only my dataset
i've a runtime datatable and i add this datatable into dataset and i want to sort dataset
Plz help me
November 20, 2008, 5:21 am

sumitrastogi said:

  Dear Friends ..... Please try this one this will definitely sort your problem of sorting.....

Dim dv As New DataView(dsEdit.Tables(0))
dv.Sort = ("ContractNo")
dsEdit.Tables.Clear()
dsEdit.Tables.Add(dv.ToTable)

Now the dataset is sorted

Sumit Rastogi
December 23, 2008, 2:21 am

chhana said:

  I think it will work very well, but how can I know how many fields are there in the Production Table, name of the fields and the datatypes. Please anybody help me, i'am a new in VS2008. i know only VB.
July 17, 2009, 6:44 am

kit code said:

  Friends, you can use

ds.Tables(0).Columns.Count (for counting number of fields)
ds.Tables(0).Columns(index).ToString (for field name)
ds.Tables(0).Columns(index).DataType.ToString (for field datatype)
August 6, 2009, 4:30 am

Raja said:

  DataRow[] rows = dtExport.Select("ColId < 40000 " i);
Above mention code used filter.
May 15, 2010, 3:15 am

Jeorge said:

  It help me too much
thank ya
July 21, 2010, 12:00 pm

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

 
 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2010 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape