4

I have a TextBox, in which I put a phrase, which is either the description of a task or the id of a task. I want to filter a list using the text from this TextBox. But when I put text into this TextBox, filtering doesn't work, and the collection in the DataGridView doesn't change.

What can be wrong?

public void BindData()
{
    var emptyBindingSource = new BindingSource();
    dataGridViewTaskList.AutoGenerateColumns = false;
    dataGridViewTaskList.DataSource = emptyBindingSource;

    var taskList = GetTasks();

    _bindingSource = new BindingSource();
    _bindingSource.DataSource=taskList.Response;

    dataGridViewTaskList.AutoGenerateColumns = false;

    dataGridViewTaskList.DataSource = _bindingSource.DataSource;

    if (dataGridViewTaskList.Columns["gridViewColumnId"] == null)
        dataGridViewTaskList.Columns.Add(new DataGridViewColumn() {Name = "gridViewColumnId"});
    else
        dataGridViewTaskList.Columns["gridViewColumnId"].DataPropertyName = "Id";

    if (dataGridViewTaskList.Columns["gridViewColumnDescription"] == null)
        dataGridViewTaskList.Columns.Add(new DataGridViewColumn() {Name = "gridViewColumnDescription"});
    else
        dataGridViewTaskList.Columns["gridViewColumnDescription"].DataPropertyName = "Description";
}

private void tbSearchedPhraseOrId_TextChanged(object sender, EventArgs e)
{
    _bindingSource.Filter = string.Format("Id = '{0}'", tbSearchedPhraseOrId.Text);
}

I added the following in BindData method and it doesn't work either:

_bindingSource.Filter = string.Format("Id LIKE '%{0}%'", "23");

Designer:

this.dataGridViewTaskList.AllowUserToAddRows = false;
this.dataGridViewTaskList.AllowUserToDeleteRows = false;
this.dataGridViewTaskList.AllowUserToOrderColumns = true;
this.dataGridViewTaskList.AllowUserToResizeRows = false;
this.dataGridViewTaskList.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
this.dataGridViewTaskList.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
this.dataGridViewTaskList.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.dataGridViewTaskList.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridViewTaskList.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.gridViewColumnId,
this.gridViewColumnDescription});
this.dataGridViewTaskList.Location = new System.Drawing.Point(6, 62);
this.dataGridViewTaskList.MultiSelect = false;
this.dataGridViewTaskList.Name = "dataGridViewTaskList";
this.dataGridViewTaskList.ReadOnly = true;
this.dataGridViewTaskList.RowHeadersVisible = false;
this.dataGridViewTaskList.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
this.dataGridViewTaskList.Size = new System.Drawing.Size(414, 488);
this.dataGridViewTaskList.TabIndex = 0;
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
user278618
  • 19,306
  • 42
  • 126
  • 196

6 Answers6

8

According to the documentation, your underlying data source (i.e. your task list) must implement the IBindingListView interface to have a working Filter property. Are you sure this is the case right now?

(As an aside, you should set the DataSource property of your DataGridView to the BindingSource object itself rather than the BindingSource.DataSource property.)

Alan
  • 6,501
  • 1
  • 28
  • 24
4

You can always check _bindingSource.SupportsFiltering to see if the BindingSource type supports filtering

Jason Roman
  • 8,146
  • 10
  • 35
  • 40
s klein
  • 41
  • 2
1

You should change:

dataGridViewTaskList.DataSource = _bindingSource.DataSource;

to

dataGridViewTaskList.DataSource = _bindingSource;

By changing _bindingSource.Filter you're not actualy changing _bindingSource.DataSource - it stays the same and because of that dataGridViewTaskList.DataSource doesn't change either. In the other hand, _bindingSource is changed, and you can bind directly to it to get that change.

Ivan Ferić
  • 4,725
  • 11
  • 37
  • 47
0

Did you try with:

_bindingSource.Filter = string.Format("gridViewColumnId = '{0}'", tbSearchedPhraseOrId.Text);

Can you define taskList structure?

watbywbarif
  • 6,487
  • 8
  • 50
  • 64
0

Try to call : _bindingSource.ResetBindings(false); after setting filter.

Also you can try to call:

dataGridViewTaskList.ResetBindings();
dataGridViewTaskList.Refresh();
dataGridViewTaskList.Update();
gunr2171
  • 16,104
  • 25
  • 61
  • 88
0

Alternative IEnumerable, List.....etc Global Variable

listBindingSource.DataSource = List();

Then simple filter the list and Re-assign it listBindingSource.DataSource = List.FindAll(t => t.x == yourValue);

  • You need to sort out the format of your answer. You can see a preview below the editing window. It is also advisable to write a better explanation. And lastly, even though your suggestion will work in practice, it's not good programming. – Shai Jul 09 '18 at 06:48