Click here to Skip to main content
Click here to Skip to main content

Filter DataGridView with the text entered in TextBox in WinForms

By , 30 Jul 2012
 

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)

About the Author

Santhosh Kumar Jayaraman
Software Developer EF
India India
Member
Started my career with Infosys 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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionGood and simple tutorialmemberMember 399685321 Jan '13 - 10:11 
Questionsearch in datagirdcontrolmemberselva_199019 Nov '12 - 18:13 
GeneralMy vote of 1memberDeyan Georgiev4 Oct '12 - 7:02 
QuestionDataTablememberSirwan Afifi30 Jul '12 - 9:15 
AnswerRe: DataTablememberSanthosh Kumar J30 Jul '12 - 18:47 

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

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