将过滤器应用于 BindingSource,但不起作用
我有一个文本框,在其中放置了一个短语,它可以是任务的描述,也可以是任务的 ID。我想使用此文本框中的文本来过滤列表。但是,当我将文本放入此 TextBox 中时,过滤不起作用,并且 DataGridView 中的集合不会更改。
有什么问题吗?
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 方法中添加了以下内容,但它也不起作用:
_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;
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
根据文档,您的基础数据源(即您的任务列表)必须实现 IBindingListView 接口才能拥有有效的 Filter 属性。你确定现在就是这种情况吗?
(顺便说一句,您应该将 DataGridView 的 DataSource 属性设置为 BindingSource 对象本身,而不是 BindingSource.DataSource 属性。)
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.)
您可以随时检查
_bindingSource.SupportsFiltering
查看BindingSource类型是否支持过滤
You can always check
_bindingSource.SupportsFiltering
to see if the BindingSource type supports filtering
您应该更改:
通过
更改
_bindingSource.Filter
您实际上并没有更改_bindingSource.DataSource
- 它保持不变,并且由于dataGridViewTaskList.DataSource
code> 也没有改变。另一方面,_bindingSource
已更改,您可以直接绑定到它以获取该更改。You should change:
to
By changing
_bindingSource.Filter
you're not actualy changing_bindingSource.DataSource
- it stays the same and because of thatdataGridViewTaskList.DataSource
doesn't change either. In the other hand,_bindingSource
is changed, and you can bind directly to it to get that change.您是否尝试过:
您可以定义taskList结构吗?
Did you try with:
Can you define taskList structure?
设置过滤器后尝试调用:
_bindingSource.ResetBindings(false);
。您也可以尝试拨打:
Try to call :
_bindingSource.ResetBindings(false);
after setting filter.Also you can try to call:
替代方案
IEnumerable、List...等全局变量
listBindingSource.DataSource = List();
然后简单过滤列表并重新分配
listBindingSource.DataSource = List.FindAll(t => tx == yourValue);
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);