将过滤器应用于 BindingSource,但不起作用 [英] Applying a filter to a BindingSource, but it doesn't work
问题描述
我有一个文本框,我在里面放了一个短语,它要么是任务的描述,要么是任务的 id.我想使用此 TextBox 中的文本过滤列表.但是当我将文本放入这个 TextBox 时,过滤不起作用,并且 DataGridView 中的集合不会改变.
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.
有什么问题?
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);
}
我在 BindData 方法中添加了以下内容,但它也不起作用:
I added the following in BindData method and it doesn't work either:
_bindingSource.Filter = string.Format("Id LIKE '%{0}%'", "23");
设计师:
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;
推荐答案
根据 文档,您的基础数据源(即您的任务列表)必须实现 IBindingListView 接口才能具有有效的 Filter 属性.您确定现在是这种情况吗?
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?
(顺便说一句,您应该将 DataGridView 的 DataSource 属性设置为 BindingSource 对象本身,而不是 BindingSource.DataSource 属性.)
(As an aside, you should set the DataSource property of your DataGridView to the BindingSource object itself rather than the BindingSource.DataSource property.)
这篇关于将过滤器应用于 BindingSource,但不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!