locked
DataGridView Filter

    Question

  • how do you filter a datagridview by controls?

    I am wanting to filter a datagrid with controls.

    What do you put in the control to change the filter of the datagrid?

    Davids Learning

    Monday, July 24, 2006 6:55 PM

Answers

  • Filtering is accomplished using the BindingSource.Filter property.  You can add a TextBox to the form and a Button.  When the Button is clicked you set BindingSource.Filter = TextBox.Text.  This applies the filter string.  The filter string is written like a WHERE clause in a SQL statement (eg. ColumnName = Value AND ColumnName2 = Value2).

    To make it easier for users, you may want to create a filter builder.  You might have a ComboBox loaded with column names, another loaded with operators (=, >, <, <>, etc) and a TextBox for the value.  The user selects a column, and operator, and types a value.  They click an Add button and you build a filter string.

    Friday, July 28, 2006 11:59 PM
  • I don't know if you have figured this out or not but do not see a reply here so....

     

    The reason your filter is not working for number 2 is the way you format the expression.

    There is a distinction in format between text and numeric filters.  remove the '% and %' from around the {1} place holder and it should work for you.

     

    hope this helps.

     

    Dave

    Saturday, May 03, 2008 8:14 PM

All replies

  • I'm having trouble understanding exactly what you're trying to accomplish.  Can you give an example scenario?
    Friday, July 28, 2006 11:41 PM
  • Filtering is accomplished using the BindingSource.Filter property.  You can add a TextBox to the form and a Button.  When the Button is clicked you set BindingSource.Filter = TextBox.Text.  This applies the filter string.  The filter string is written like a WHERE clause in a SQL statement (eg. ColumnName = Value AND ColumnName2 = Value2).

    To make it easier for users, you may want to create a filter builder.  You might have a ComboBox loaded with column names, another loaded with operators (=, >, <, <>, etc) and a TextBox for the value.  The user selects a column, and operator, and types a value.  They click an Add button and you build a filter string.

    Friday, July 28, 2006 11:59 PM
  • And what about Integers, not strings?

    I have an SQL datasource bindinged to a datagrid.
    I can filter the Column NAME, but how can i filter an INT type column?

    I have two types of filter:

    1. This is working with string Column

    Try
                'sets filter string WHERE clause in the form "Where COLUMNNAME like '@FilterText'"
                Me.JatekosBindingSource.Filter = String.Format("{0} like '%{1}%'", Me.VizilabdaDataSet.jatekos.NAMEColumn.ColumnName, Me.TextBox1.Text)
    Catch ex As Exception
                MsgBox("ERROR")
    End Try


    2. Not working

    Try
                    'sets filter string WHERE clause in the form "Where COLUMNNAME like '@FilterText'"
                    Me.JatekosBindingSource.Filter = String.Format("{0} = '%{1}%'", Me.VizilabdaDataSet.jatekos.INTEGERColumn.ColumnName, CStr(Me.ComboBox1.SelectedIndex))
    Catch ex As Exception
                    MsgBox("ERROR")
    End Try

    In second event, i get the messagebox with Error message. How can i filter an integer type column?
    Monday, March 24, 2008 4:08 PM
  • Are you filtering the data that was entered into the datagrid before binding,

     

    Or

     

    Are you filtering the database and wanting to show only certain numbers that already exist?

     

    Davids Learning

     

    Monday, March 24, 2008 5:50 PM
  • I want to show only certain rows that already exist.

    Meanwhile I get the solution:

    Me.JatekosBindingSource.Filter = String.Format("{0} = '{1}'", Me.VizilabdaDataSet.jatekos.INTEGERColumn.ColumnName, CStr(Me.ComboBox1.SelectedIndex))
    Monday, March 24, 2008 7:07 PM
  • I don't know if you have figured this out or not but do not see a reply here so....

     

    The reason your filter is not working for number 2 is the way you format the expression.

    There is a distinction in format between text and numeric filters.  remove the '% and %' from around the {1} place holder and it should work for you.

     

    hope this helps.

     

    Dave

    Saturday, May 03, 2008 8:14 PM