0

I have this scenario where I need to search my datagridview using the textbox I have, I have tried copying other sites but what it gives me is errors, new columns and more errors, my codes goes like this for loading my datas from the database to the gridview

private void cashier_update_Load(object sender, EventArgs e)     
{        
    con.ConnectionString = @"";
    con.Open();    
    SqlDataAdapter sda = new SqlDataAdapter("SELECT entry_no,prod_id, prod_name,      wantiti,item_status FROM Table_3 Where item_status= 'ACTIVE'", con);    
    DataTable dt = new DataTable();   
    sda.Fill(dt);   
    dataGridViewX1.Rows.Clear();    
    foreach (DataRow item in dt.Rows)    
    {   
        int n = dataGridViewX1.Rows.Add();    
        dataGridViewX1.Rows[n].Cells[0].Value = item[0].ToString();    
        dataGridViewX1.Rows[n].Cells[1].Value = item[1].ToString();    
        dataGridViewX1.Rows[n].Cells[2].Value = item[2].ToString();    
        dataGridViewX1.Rows[n].Cells[3].Value = item[3].ToString();   
    }     
    con.Close();   
}  

Now what I need is to search the column 2 which is prod_name via key_up or textchange.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
  • http://www.youtube.com/watch?v=z0HwGx10xz8 this one but i get new columns, – Justine Wenen Vega Oct 16 '14 at 14:17
  • The method in the movie is a extremely power hungry approach. You're setting up a connection and querying the database with every key press. Its better to cache the data in your RAM. – Nick Otten Oct 16 '14 at 14:26

1 Answers1

0

You could use a filter object to do this. but first you will have to bind your data to the gridview using a data source (dataGridViewX1.datasource = dt; in your case, you can remove your foreach loop as columns and rows are added automatically) after that you can filter your data using a bindingsource.filter.

For more information about the filter you could look at the following answer: How to filter DataGridView in C# Win Forms?

full documentation can be found here: http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.filter(v=vs.110).aspx

Community
  • 1
  • 1
Nick Otten
  • 702
  • 7
  • 17
  • edit: already did it and it worked, thanks, anyway... do you know how to change my column name? coz it shows my database column for it as wantiti but i want it to display quantity, thanks – Justine Wenen Vega Oct 16 '14 at 14:29
  • dgDatagrid.columns["name_used_in_database"].name = "new_name". You could also use a index to select your column but names are saver when you wan't to swap the order of columns – Nick Otten Oct 16 '14 at 14:32