Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do I search in a Datagridview from a Textbox in a C# Windows App?
Posted

look man if you want to use a "search" in your grid i advise you to use telerik grid (Telerik Controls see them in the internet) it contain all the kind of search via grid in all column (really you can stop them in some columns) see this link or thislink

best regards
 
Share this answer
 
Write search query in Textbox's Text_Changed Event.
use DataAdapter and Dataset

C#
dim DataAdapter as new OleDbDataAdapter("select * from [TableName] where [ColumnName] like %'" & Textbox1.Text &"'%",con);

Dim ds as DataSet;

adp.fill(ds);

DataGridView.Datasource=ds.Tables(0);
 
Share this answer
 
You can easily search the datagridview using a textbox while typing the search words. Try the below simple example. It works perfectly with me.
DataTable dt = new DataTable();
DataView dv = null;

C#
private void Test()
{

C#
dt.Columns.Add("id", typeof(Int32));
dt.Columns.Add("code", typeof(String));

DataRow dr;

C#
dr = dt.NewRow();
dr[0] = 1;
dr[1] = "abc";
dt.Rows.Add(dr);

C#
dr = dt.NewRow();
dr[0] = 2;
dr[1] = "def";
dt.Rows.Add(dr);

C#
dv = dt.DefaultView;
dataGridView1.DataSource = dv;

}
C#
private void textBox1_TextChanged(object sender, EventArgs e)
{
 try
 {
    dv.RowFilter = "code like'%" + textBox1.Text.Trim() +  "%'";
 }
 catch { }
}
 
Share this answer
 
Thanks for all your help. Here is one of the sulotions.

C#
foreach (DataGridViewRow currentRow in dataGridView1.Rows)
        {
            if (currentRow.Cells["AdminnrID"].Value.ToString() == textBox1.Text)
            {
                currentRow.Selected = true;
                break;
            }


AdminnrID is the column name you want to search
 
Share this answer
 
Comments
Khaled Fattoum 25-Jul-12 18:03pm    
{{{Error 1 'Super_market.Login.dataGridViewRow' is a 'field' but is used like a 'type'}}}
this error appears
which dataGridView should be connected to my table? "dataGridViewRow" or "dataGridView1"???
and how should i define "currentRow"???
thnx a lot for helping

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900