Click here to Skip to main content
11,581,510 members (66,099 online)
Click here to Skip to main content

Filter DataGridView with the text entered in TextBox in WinForms

, 30 Jul 2012 CPOL 47K 1.7K 6
Rate this:
Please Sign up or sign in to vote.
How to filter a DataGridView when we type some values in a textbox.

Introduction

In this article, let us see how to filter a DataGridView when we type some values in a textbox.

Using the code

  1. Create a new Windows application. Add a DataGridView and a TextBox control.
  2. I have an XML file which am going to bind to the GridView.
  3. XML File 1
    <?xml version="1.0" standalone="yes" ?> 
    - <NewDataSet>
    - <Table1>
      <Server>Server1</Server> 
      <Database>Database1</Database> 
      </Table1>
    - <Table1>
      <Server>Server2</Server> 
      <Database>Database2</Database> 
      </Table1>
    - <Table1>
      <Server>Server3</Server> 
      <Database>Database3</Database> 
      </Table1>
    </NewDataSet>
  4. Create two properties of type DataSet and DataView as below.
  5. public DataSet ds
    {
        get;
        set;
    }
    
    public DataView dv
    {
        get;
        set;
    }
  6. In the Form constructor, create an object of DataSet and DataView.
  7. public Form1()
    {
        InitializeComponent();
        ds = new DataSet();
       dv = new DataView();
    }
  8. Now in the form load, read the XML file and save the values into the DataSet. Then load the tables inside DataView with DataTable inside DataSet. Now bind this DataView to the GridView.
  9. private void Form1_Load(object sender, EventArgs e)
    {
        string path = "C:\\XMLFile1.xml";
        ds.ReadXml(path);            
        dv.Table = ds.Tables[0];
        dataGridView1.DataSource = dv; 
    }
  10. Now in the text_changed property of the textbox, add the below code. Here I am filtering it with the column Server.
  11. dv.RowFilter = "Server like '%" + textBox1.Text + "%'";
    dataGridView1.DataSource = dv;
  12. Run the application and check.
    1. Form load.
    2. Valid data in column Server.
    3. Invalid data in column Server.

I have attached the complete source code also.

License

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

Share

About the Author

Santhosh Kumar Jayaraman
Software Developer Radiometer
India India
Currently i am trying to learn all about javascripts . I have experience in Microsoft technologies like WPF, WCF, ASPNET, WinForms,Silverlight, VB.NET, C-Sharp, Entity framework,SSRS, LINQ, Extension methods and SQL server. I started with my career in Infosys in the year 2009 and i worked with Trigent, Education First (EF) in the recent years before joining my current company Radiometer.

You may also be interested in...

Comments and Discussions

 
QuestionError in code Pin
Member 81185167-Feb-15 17:39
memberMember 81185167-Feb-15 17:39 
QuestionWhy Im getting error at dataGridView4.RowFilter Pin
n shiva Ram22-Oct-14 0:18
membern shiva Ram22-Oct-14 0:18 
QuestionGood and simple tutorial Pin
Member 399685321-Jan-13 10:11
memberMember 399685321-Jan-13 10:11 
Questionsearch in datagirdcontrol Pin
selva_199019-Nov-12 18:13
memberselva_199019-Nov-12 18:13 
GeneralMy vote of 1 Pin
Deyan Georgiev4-Oct-12 7:02
memberDeyan Georgiev4-Oct-12 7:02 
QuestionDataTable Pin
Sirwan Afifi30-Jul-12 9:15
memberSirwan Afifi30-Jul-12 9:15 
AnswerRe: DataTable Pin
Santhosh Kumar J30-Jul-12 18:47
memberSanthosh Kumar J30-Jul-12 18:47 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150603.1 | Last Updated 30 Jul 2012
Article Copyright 2012 by Santhosh Kumar Jayaraman
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid