Click here to Skip to main content
15,881,139 members
Articles / Programming Languages / C#

.NET DataSet filter demonstration

Rate me:
Please Sign up or sign in to vote.
2.20/5 (7 votes)
26 Jul 2007CPOL2 min read 58.1K   763   17   5
This article explains how to filter rows in a DataSet/DataTable. The example provided will help you get information faster.

Screenshot - DTFilter.jpg

Introduction

This article is a demonstration of filtering a DataSet/DataTable with the Select method. This article is for beginners and will help them in learning the language basics.

Background

This is just a hobby program which I thought will be helpful for .NET beginners.

Using the code

The sample code contains only a WinForm which loads some rows with random data. I have added a ListBox with ready made filter expressions and there is a TextBox which will help you to enter user-defined filter expressions.

There is a DataTable I use in this application as a global - parent datatable. I apply filter expressions on this. This DataTable is a dynamically created one. The LoadData() method is used to fill random data into this DataTable.

C#
DataTable dt = new DataTable();
private void Form1_Load(object sender, EventArgs e)
{
    dt.Columns.Add("A");
    dt.Columns.Add("B");
    LoadData();
}

The LoadDate() method is shown below. Notice the usage of Random(). The same method is associated with the 'Reset Data' button.

C#
private void LoadData()
{
    dt.Rows.Clear();
    Random r = new Random();
    for (int i = 0; i < 10; i++)
    {
        DataRow dr = dt.NewRow();
        dr["A"] = r.Next(0, 1000);
        dr["B"] = r.Next(0, 1000);
        dt.Rows.Add(dr);
    }

    dataGridView1.DataSource = dt;
}

Next is the most important method in this application. This is the method which gives 'life' to this application: the DoFilter() method. You can pass the filter expression as a string to this method and the effect can be seen on the DataGridView attached.

C#
private void DoFilter(string filter)
{
    DataTable dt1 = dt.Clone();
    try
    {
        foreach (DataRow dr in dt.Select(filter))
    {
        dt1.ImportRow(dr);
    }
        dataGridView1.DataSource = dt1;
    }
    catch
    // Warning: Non-standard! - without proper catching of exception

    {
        MessageBox.Show("Error in filter expression");
    }
}

If you give an invalid filter expression, the application will show an 'Error in filter expression' message. You may have noticed the dt.Select(filter) in the above code. As you know, this is the key of this article which does the filtering job. Since dt.Select() returns an array of DataRows, I used a temporary table with the same structure (dt.Clone()) to import the filtered results and bind it to DataGridView.

Generic method

Here is a generic method overview for your projects. Please do not use this as it is since I wrote this function/method as a part of writing this article and it is not properly tested.

C#
private DataTable DoDataTableFilter(DataTable dt, string filter)
{
    DataTable dt1 = dt.Clone();
    foreach (DataRow dr in dt.Select(filter))
    {
        dt1.ImportRow(dr);
    }
    return dt1;
}

Points of interest

Note that since this is a very simple and quickly-made application, I did not do proper exception handling and object disposals. Standard code always must follow proper coding standards.

Happy coding.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect ORION INDIA SYSTEMS
India India
Praveen.V.Nair - aka NinethSense - PMP, Microsoft MVP - is working as a Head of Technology and Architecture at Orion India Systems, Kochi, India. He has been playing with electronics from the age of 10 and with computers from the age of 14. He usually blogs at http://blog.ninethsense.com/.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Syed J Hashmi8-Jul-11 22:21
Syed J Hashmi8-Jul-11 22:21 
GeneralGood! Pin
Sunvory29-Dec-09 18:40
Sunvory29-Dec-09 18:40 
GeneralSimple but of Great Use Pin
Sandeepan23-Aug-07 23:15
Sandeepan23-Aug-07 23:15 
QuestionWhy not use a DataView? Pin
David Hay30-Jul-07 20:43
David Hay30-Jul-07 20:43 
AnswerRe: Why not use a DataView? Pin
Praveen Nair (NinethSense)30-Jul-07 21:45
Praveen Nair (NinethSense)30-Jul-07 21:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.