Question

I have a DataGridView in a form which displays a client list with the columns ID, First Name, Last Name, Address. I also included a TextBox to perform the search query. I want to filter my DataGridView based from the given columns using a single TextBox (like a multi data filter/search where I can select from those four columns by typing on a single TextBox).

The question is: am I required to create a binding source for here (I populate my DataGridView using sql database) or is there a way to create filters without having to add a binding source?

Était-ce utile?

La solution

You don't populate your grid using a sql database. You populate your grid from a SQL Server database suing something else in between, e.g. a SqlDataAdapter and a DataTable.

A BindingSource is supposed to be a one-stop shop for working with bound data so, while you don't have to use one, I would recommend doing so. Whether it can help you filter your data depends on what it's bound to. The BindingSource doesn't actually do the work of filtering itself but rather passes the work on to the underlying IBindingListView implementation if there is one. For instance, if the underlying data source is a DataTable then the RowFilter of its DefaultView will be used. You could set the DefaultView.RowFilter yourself.

If you want to do all the heavy lifting yourself then you could also search the grid yourself and then hide the rows that don't match. Those rows would still exist though, so you'd have to take that into account when using the data in code.

Autres conseils

You have to create a query to retrive a data using like keyword by passing parameter value in form

Example:

  1. pass the parameter from

    form(cmd.parameters.add(nvarchar, 20).value = "textbox.text")
    
  2. create a stored procedure

    CREATE PROCEDURE procedure_name
        @searchvalue varchar(20)
    AS
    BEGIN
        SELECT
            ID, FirstName, LastName, Address 
        FROM
            tablename 
        WHERE
            FirstName LIKE '%@SearchTerm%'
    END
    

    This above stored procedure will retrieve all the rows where the FirstName column value corresponds to that name

  3. Then pass the result to gridview

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top