Click here to Skip to main content
Click here to Skip to main content
Go to top

Filter DataGridView with the text entered in TextBox in WinForms

, 30 Jul 2012
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 Education First
India India
Started my career with Infosys in 2009 and currently working with Education First. I have great passion towards Microsoft technologies. 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.

Comments and Discussions

 
QuestionGood and simple tutorial PinmemberMember 399685321-Jan-13 10:11 
Questionsearch in datagirdcontrol Pinmemberselva_199019-Nov-12 18:13 
GeneralMy vote of 1 PinmemberDeyan Georgiev4-Oct-12 7:02 
QuestionDataTable PinmemberSirwan Afifi30-Jul-12 9:15 
AnswerRe: DataTable PinmemberSanthosh 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 | Mobile
Web04 | 2.8.140916.1 | Last Updated 30 Jul 2012
Article Copyright 2012 by Santhosh Kumar Jayaraman
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid