Introduction
In this article, let us see how to filter a DataGridView when we type some values in a textbox.
Using the code
- Create a new Windows application. Add a
DataGridView and a
TextBox control.

- I have an XML file which am going to bind to the
GridView.
XML File 1
="1.0" ="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>
- Create two properties of type
DataSet and DataView as below.
public DataSet ds
{
get;
set;
}
public DataView dv
{
get;
set;
}
- In the Form constructor, create an object of
DataSet and
DataView.
public Form1()
{
InitializeComponent();
ds = new DataSet();
dv = new DataView();
}
- 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.
private void Form1_Load(object sender, EventArgs e)
{
string path = "C:\\XMLFile1.xml";
ds.ReadXml(path);
dv.Table = ds.Tables[0];
dataGridView1.DataSource = dv;
}
- Now in the
text_changed property of the textbox, add the below code. Here
I am filtering it with the column Server.
dv.RowFilter = "Server like '%" + textBox1.Text + "%'";
dataGridView1.DataSource = dv;
- Run the application and check.
- Form load.

- Valid data in column Server.

- Invalid data in column Server.

I have attached the complete source code also.
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.