Home > Blockchain >  How can i get a proper Filter function in my DataGridView?
How can i get a proper Filter function in my DataGridView?

Time:12-23

I'm kind of new to Porgramming with WinForms and I want to get a proper Filter Function...

Here is what i have so far.

Picture of my DataGridView

And now I want to filter by Cola for example. So if you type C only Cola and Chips should pop up.

Thank you a lot for helping if you can.

CodePudding user response:

Do any parts of this you hven't already done:

  • Add a DataSet type of file to your project. Call it some sensible name, not DataSet1
  • Open it, and a design surface appears, right click it and Add.. DataTable
  • Rename the table to Stock
  • Right click it and Add.. Column, call it Name
  • Add another column called Price (and set the datatype to be float)
  • Switch to the form designer
  • Open the Data Sources window (View menu, Other Windows) and drag the Stock node from the tree onto the form
  • A datagridview, bindingnavigator, bindingsource and dataset appear on the form/in the bottom tray
  • Add a textbox and call it searchTextBox
  • Add a button and cal it searchButton
  • Double click the button to add an event handler and switch to code view
  • Add some code into the click handler:
    private void SearchButton_Click(obejct sender, EventArgs e){
      if(string.IsNullorWhiteSpace(searchTextBox.Text))
        stockBindingSource.RemoveFilter();
      else
        stockBindingSource.Filter = "[Name] LIKE '"   searchTextBox.Text.Replace("'", "''")   "'";
    }

  • Add some code into the constructor:
    somesensiblenameDataset.Stock.AddStockRow("Cola", 2.2);
    somesensiblenameDataset.Stock.AddStockRow("Fanta", 2.2);
    somesensiblenameDataset.Stock.AddStockRow("Ice-Tea", 3.9);
    ...

Run the app, type Fanta and click Search. Type F* and click Search

  • Related