优化绑定的DataGridView搜索功能 [英] Optimize binded DataGridView search feature

查看:189
本文介绍了优化绑定的DataGridView搜索功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我有我的代码运行完全正常,但它看起来很讨厌。它的目的是从我的数据库符合搜索条件的搜索和显示工人。我有一个 DataGridView的我的表上充斥着从我的数据库databinded工人。我按搜索按钮后,我只想显示匹配从文本框/组合框标准的工人的名单。 。在形式如果他们是空的它再次显示完整列表。

I've got my code running perfectly fine, but it looks nasty. Its purpose is to search and display workers from my database that match search criteria. I've got a DataGridView on my form filled with workers databinded from my database. After I press Search button I only want to display the list of workers matching the criteria from TextBoxes/ComboBoxes. If they're empty it's displaying a full list once again.

搜索功能:

代码:

private void btnSearch_Click(object sender, EventArgs e)
{
    FillList();
    List<Worker> list = new List<Worker>();
    bool match = false;

    foreach (var worker in workerBindingSource)
    {
        if (txtName.Text.Length > 0 && !((Worker)worker).Name.ToLowerInvariant().Contains(txtName.Text.ToLowerInvariant()))
        {
            match = false;
            continue;
        }
        else
            match = true;

        if (txtLastName.Text.Length > 0 && !((Worker)worker).LastName.ToLowerInvariant().Contains(txtLastName.Text.ToLowerInvariant()))
        {
            match = false;
            continue;
        }
        else
            match = true;

        if (txtOIB.Text.Length > 0 && !((Worker)worker).OIB.ToLowerInvariant().Contains(txtOIB.Text.ToLowerInvariant()))
        {
            match = false;
            continue;
        }
        else
            match = true;

        if (txtLocation.Text.Length > 0 && !((Worker)worker).Location.ToLowerInvariant().Contains(txtLocation.Text.ToLowerInvariant()))
        {
            match = false;
            continue;
        }
        else
            match = true;

        if (txtAddress.Text.Length > 0 && !((Worker)worker).Address.ToLowerInvariant().Contains(txtAddress.Text.ToLowerInvariant()))
        {
            match = false;
            continue;
        }
        else
            match = true;

        if (txtPhoneNumber.Text.Length > 0 && !((Worker)worker).PhoneNumber.ToLowerInvariant().Contains(txtPhoneNumber.Text.ToLowerInvariant()))
        {
            match = false;
            continue;
        }
        else
            match = true;

        if (!cboProfessionalQualification.SelectedValue.ToString().Equals("Empty") && !((Worker)worker).ProfessionalQualification.ToString().ToLowerInvariant().Contains(cboProfessionalQualification.SelectedValue.ToString().ToLowerInvariant()))
        {
            match = false;
            continue;
        }
        else
            match = true;

        if (!cboDegree.SelectedValue.ToString().Equals("Empty") && !((Worker)worker).Degree.ToString().ToLowerInvariant().Equals(cboDegree.SelectedValue.ToString().ToLowerInvariant()))
        {
            match = false;
            continue;
        }
        else
            match = true;

        if (match)
        {
            list.Add(WorkerDAO.ReadWorker(((Worker)worker).ID));
        }
    }

    SortableBindingList<Worker> sortableList = new SortableBindingList<Worker>(list);
    workerBindingSource.DataSource = sortableList;
}




中的代码

一切 TXT前缀是文本框和一切与CBO的前缀是组合框组合框包含某些枚举用户可以挑选。如果组合框正在显示枚举值的的这意味着用户拿起什么(相同字符串的长度为零值)。 SortableBindingList 仅仅是在列标题点击,没有在这种情况下重要的分拣目的。 ReadWorker 方法只返回,如果他匹配所有搜索的价值观和他的然后添加到列表我显示在 DataGridView的工人之后。 FillList 的方法是从数据库工人的整个列表设置为 DataBindingSource


Everything in code with "txt" prefix is TextBox and everything with "cbo" prefix is ComboBox. ComboBoxes contain certain enums that user can pick. If ComboBox is displaying enum value "Empty" it means user picked nothing (same as zero length value of string). SortableBindingList is just for sorting purpose on column header click, nothing important in this case. ReadWorker method is returning a worker only if he's matching all searching values and he's then added to list I display on DataGridView after. FillList method is setting whole list of workers from database to DataBindingSource.

ReadWorker方式:

public static Worker ReadWorker(int workerID)
{
    var worker = ReadEverything().Where(x => x.ID == workerID).FirstOrDefault();

    return worker;
}






方法 ReadEverything 返回从数据库中所有员工的列表。


Method ReadEverything returns list of all workers from database.

我正在寻找一个解决方案,LINQ(或任何其他更好的解决方案),以减少我的代码,并大幅使其更具可读性,但因为我还是很新鲜的编程和LINQ我不能找出自己。所以我想如果你们能帮助我一点,或至少点我在正确的方向。

I'm looking for a LINQ solution (or any other better solution) to reduce my code drastically and make it more readable, but since I'm still pretty fresh to programming and LINQ I can't figure it out myself. So I wondered if you guys could help me a bit or at least point me at right direction.

提前感谢!

Thanks in advance!

推荐答案

如果您的datagridview已经从数据库中的所有数据,我建议你不要去数据库再次搜索,因为你已经把所有的数据。只是过滤它在内存中,并显示它。

If your datagridview already has all the data from database, I'll suggest you not to go to database for search again, because you already have all the data. Just filter it in memory and show it.

如果在你需要打的数据库出于某种原因,没有理由,从数据库中获取的所有数据,并进行过滤在客户端,只需通过workerId存储过程,让SP加适量的地方条件并返回过滤后的数据。

If at all you need to hit database for some reason, there is no reason to get all the data from database and filter it in client side, just pass the "workerId" to stored procedure, let the SP add proper where condition and return the filtered data.

要在内存中实现使用绑定源过滤

To implement in memory filtering using Binding Source:

bindingSource.Filter = "columnname = 'value'";

private void button1_Click(object sender, EventArgs e)
{
    string searchValue = textBox1.Text;

     dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
     bindingSource.Filter = string.Format("{0} = '{1}'","YourColumnName", searchValue );
     //here you can do selection if you need
}

To remove filter use the following

bindingSource.RemoveFilter();
//or
bindingSource.Filter = null;



最初发布答案的这里

这篇关于优化绑定的DataGridView搜索功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆