搜索并过滤datagridview

时间:2013-01-27 16:22:28

标签: c# datagridview

我使用以下功能

加载gridview
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 ;
}

但它仍然不起作用

3 个答案:

答案 0 :(得分:1)

在linq语句中添加where子句。

BTW:您的所有订购,过滤和放大预测是在记忆中完成的。您在应用任何此类记录集之前将整个记录集加载到内存中,因为您使用的是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();

答案 1 :(得分:1)

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

您使用or的{​​{1}} operator在C#和{{3}中表示为SQL || }表示为|的{​​{1}}。以下是使用LIKE .Contains

进行过滤的一个很好的示例
SQL

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

.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();

答案 2 :(得分:0)

所以我解决了下面代码的问题

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