Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to filter the data on grid view which i put in textbox. I don't know the code for it. i tried the below code. Please help me in doing the filter data where the datas are already present.

i'm using foreach loop. i don't know what to use in place of
int n = dataGridView1.Rows.Add();
please help me

What I have tried:

C#
private void textBox3_TextChanged(object sender, EventArgs e)
          {
              SearchData(textBox3.Text);
        }

        public void SearchData(string search)
        {
            SqlConnection con = Connection.GetConnection();
            con.Open();
            string query = "Select * FROM [Stock] WHERE [CompDiscription] Like '%" + search + "%'";
            SqlDataAdapter sda = new SqlDataAdapter(query, con);
            DataTable dt = new DataTable();
            sda.Fill(dt);            
            foreach (DataRow item in dt.Rows)
            {
                            
              int n = dataGridView1.Rows.Add();
              
              dataGridView1.Rows[n].Cells[0].Value = item["APartNo"].ToString();
              dataGridView1.Rows[n].Cells[1].Value = item["CompPartNo"].ToString();
              dataGridView1.Rows[n].Cells[2].Value = item["CompDiscription"].ToString();
              dataGridView1.Rows[n].Cells[3].Value = item["Quantity"].ToString();
              dataGridView1.Rows[n].Cells[4].Value = item["CompanyName"].ToString();
              dataGridView1.Rows[n].Cells[5].Value = item["BinNo"].ToString();
              this.dataGridView1.Visible = true;

            }
            con.Close();
        }
Posted
Updated 5-Mar-20 19:59pm
v3

1 solution

Hi,

please try following solution,

C#
<pre>private void Form1_Load(object sender, EventArgs e)
{
    dataGridView1.DataSource = this.PopulateDataGridView();
}
 
private void txtName_KeyUp(object sender, KeyEventArgs e)
{
    dataGridView1.DataSource = this.PopulateDataGridView();
}
 
private DataTable PopulateDataGridView()
{
    string query = "SELECT CustomerID, ContactName, Country FROM Customers";
    query += " WHERE ContactName LIKE '%' + @ContactName + '%'";
    query += " OR @ContactName = ''";
    string constr = @"Data Source=.\SQL2005;Initial Catalog=Northwind;User ID=sa;Password=pass@123";
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand(query, con))
        {
            cmd.Parameters.AddWithValue("@ContactName", txtName.Text.Trim());
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            {
                DataTable dt = new DataTable();
                sda.Fill(dt);
                return dt;
            }
        }
    }
}
 
Share this answer
 

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