本文介绍了在 DataGridView 中使用 BindingSource 的 C# 过滤器对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在过滤 BindingSource 中的数据时遇到问题.我在 DataGridView 中列出数据.

I am having a problem with filtering my data in a BindingSource. I am listing the data in a DataGridView.

class client
{

    public int id { get; set; }
    public String name { get; set; }


    public client(String name)
    {
        this.name = name;
        this.id = 0;
    }
}

BindingSource clients = new BindingSource();

clients.Add(new client("Test1"));
clients.Add(new client("Test2"));

dataGridView_clients.AutoGenerateColumns = false;
dataGridView_clients.ColumnCount = 2;

dataGridView_clients.Columns[0].Name = "id";
dataGridView_clients.Columns[0].DataPropertyName = "id";

dataGridView_clients.Columns[1].Name = "name";
dataGridView_clients.Columns[1].DataPropertyName = "name";

dataGridView_clients.DataSource = clients_source;

clients.Filter = string.Format("Name = 'Test1'");
dataGridView_clients.Refresh();

我做错了什么?过滤器不起作用,它显示在最后两个客户端中.

What am I doing wrong? The filter doesn't work, it is being shown in the last two clients.

推荐答案

你可以在数据绑定前过滤一个列表:

You can filter a list before data binding:

List<client> clients = new List<client> { new client("Test1"), new client("Test2") };

List<client> filtered = clients.FindAll(c => c.name == "Test2");

dataGridView1.DataSource = filtered;

这篇关于在 DataGridView 中使用 BindingSource 的 C# 过滤器对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 08:04