在数据网格视图上搜索和筛选

本文关键字:搜索 筛选 视图 数据 数据网 网格 | 更新日期: 2023-09-27 17:56:26

>我使用以下函数加载网格视图以形成

private DataClasses1DataContext db_cooperations = new DataClasses1DataContext();
private int numberOfObjectsPerPage = 20;
private int CurrentPageIndex = 1;
Func<IEnumerable<cooperations>, IEnumerable<cooperations>> ordering = t => t.OrderBy(i => i.name);
private void daftarKoperasi_Load(object sender, EventArgs e)
{
    GetData(CurrentPageIndex);
    dataGridProperty();
}
 private void GetData(int page)
{
    page = page - 1;
    db_cooperations.Log = Console.Out;  
    var query = (from c in ordering(db_cooperations.cooperations)
                select new { c.id, c.name, c.phone, c.email, c.city })
                .Skip(numberOfObjectsPerPage * page).Take(numberOfObjectsPerPage).ToList();

    dataGridView1.DataSource = query ;
}

然后我想根据名称搜索数据,同时过滤城市。任何线索我应该进一步做什么?

更新

所以我更新我的代码如下

private void GetData(int page)
{
    page = page - 1;
    db_cooperations.Log = Console.Out;  
    var query = (from c in ordering(db_cooperations.cooperations)
                select new { c.id, c.name, c.phone, c.email, c.city })
                .Skip(numberOfObjectsPerPage * page).Take(numberOfObjectsPerPage).ToList();

    if (String.IsNullOrEmpty(searchTxt.Text) == false)
    {
        query.Where(c => c.name.ToLower().Contains(searchTxt.Text.ToLower()));
    }
    if (cityCB.SelectedIndex > -1)
        {
        query.Where(c => c.city.ToLower().Equals(cityCB.Text.ToLower()));
    }
    dataGridView1.DataSource = query ;
}

但它仍然不起作用

在数据网格视图上搜索和筛选

为了根据name过滤(搜索)数据,同时过滤Linq to Sql中的city

您可以使用or SQL 的运算符,这些运算符在 C# 和 中表示为 || |。包含表示为 SQLLIKE。下面是有关使用 LINQ 包含不区分大小写.Contains进行筛选的一个很好的例子

        var query = (from c in ordering(db_cooperations.cooperations)
                     select new { c.id, c.name, c.phone, c.email, c.city })
                     .where(c => c.name.ToLower().Contains(searchTxt.Text.ToLower()) || 
                            c => c.city.ToLower().Contains(searchTxt.Text.ToLower()))
            .Skip(numberOfObjectsPerPage * page).Take(numberOfObjectsPerPage).ToList();

PS:可能是我会想念你的问题,但你这样去,

.where(c => c.name.ToLower().Contains(searchTxt.Text.ToLower()) && 
                                c => c.city == cityCB.Text)

编辑:

从您的更新中,

   if (String.IsNullOrEmpty(searchTxt.Text) == false)
    {
      query = query.Where(c => c.name.ToLower().Contains(searchTxt.Text.ToLower())).ToList();
    }
    if (cityCB.SelectedIndex > -1)
    {
       query =  query.Where(c => c.city.ToLower().Equals(cityCB.Text.ToLower())).ToList();
    }

where 子句添加到 linq 语句中。

顺便说一句:你所有的排序、过滤和投影都是在内存中完成的。 在应用任何记录集之前,您将整个记录集加载到内存中,因为您使用的是IEnumerable<Corproations>而不是IQueryable<Corporations>

(from c in db_cooperations.cooperations 
where c.Name == name && c.City == city
order by c.Name)
.Skip(pagesize * page).Take(pagesize).AsEnumerable();
所以我

在下面的代码中解决问题

var query = (from c in ordering(db_cooperations.cooperations)
                         select new { c.id, c.name, c.phone, c.email, c.city })
                         .Skip(numberOfObjectsPerPage * page).Take(numberOfObjectsPerPage);
if (String.IsNullOrEmpty(searchTxt.Text) == false && searchTxt.Text.Trim() != "Search....")
    {
        query = from c in query
        where c.name.ToLower().Contains(searchTxt.Text.ToLower())
        select new { c.id, c.name, c.phone, c.email, c.city })
    }
if (kotaCB.SelectedIndex > -1)
    {
        query = from c in query
                where c.kota.ToLower().Equals(kotaCB.Text.ToLower())
                select new { c.id, c.name, c.phone, c.email, c.city });
        }
dataGridView1.DataSource = query.ToList();

谢谢大家帮助我@spajce,@Jason