dataGridView中的multifilter如何用两个文本框?

时间:2016-03-02 04:43:04

标签: c# mysql visual-studio-2015

例如,当具有不同姓氏的相同名字时 过滤名称并写入姓氏过滤器,但保持名称过滤。

文本框示例:

连接:

public void read_data(string query, ref DataSet principal, string tabla)
{
    try
    {
       string cadena = "Server=0.0.0.0; Database=DB; Uid=user; Pwd=pass";
       MySqlConnection cn = new MySqlConnection(cadena);
       MySqlCommand cmd = new MySqlCommand(query, cn);
       cn.Open();
       MySqlDataAdapter da = new MySqlDataAdapter(cmd);
       da.Fill(principal, tabla);
       cn.Close();
    }
    catch (Exception ex)
    {

    }
}

加载dataGridView:

private void Form1_Load(object sender, EventArgs e)
{
    this.read_data("Select * From reg", ref result, "reg");
    this.filtro = ((DataTable)result.Tables["regi"]).DefaultView;
    this.dataGridView1.DataSource = filtro;
}

事件:

private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
    string data_output = " ";
    string[] search_word = this.textBox1.Text.Split(' ');

    foreach (string word in search_word)
    {
        if (data_output.Length == 0)
        {
            data_output = "(FirstName LIKE '%" + word + "%')";
        }
        else
        {
            data_output += "(FirstName LIKE '%" + word + "%')";
        }
    }
    this.filtro.RowFilter = data_output;
}

1 个答案:

答案 0 :(得分:1)

这是实现您的要求的一种方法。

创建一个通用函数,从两个文本框中调用

    private string GetFilterExpression(string sFilterExprValue, string sFilterFieldName)
    {
        string sFilterExpr = "";
        string[] search_word = sFilterExprValue.Split(' ');

        foreach (string word in search_word)
        {
            if (sFilterExpr.Length == 0)
            {
                sFilterExpr = "(" + sFilterFieldName + " LIKE '%" + word + "%')";
            }
            else
            {
                sFilterExpr += " AND (" + sFilterFieldName + " LIKE '%" + word + "%')";
            }
        }

        return sFilterExpr;
    }

从两个文本框的按键事件中调用公共函数,如下所示;

带有名字过滤器的文本框1

    private void textBox1_KeyUp(object sender, KeyEventArgs e)
    {
        // Text box to enter Firstname filter
        string sFilter = GetFilterExpression(this.textBox1.text, "FirstName");

        if (!this.textBox2.text)
        {
            string sLastNameFilter = GetFilterExpression(this.textBox2.text, "LastName");

            if (!String.IsNullOrEmpty(sLastNameFilter))     // Concat the Last Name Filter 
                sFilter +=  String.IsNullOrEmpty(sFilter) ? "" : " AND " + sLastNameFilter;
        }

        this.filtro.RowFilter = sFilter;
    }

带有姓氏过滤器的文本框2

    private void textBox2_KeyUp(object sender, KeyEventArgs e)
    {
        // Text box to enter Lastname filter
        // Assume the field storing last name is 'Lastname'
        string sFilter = GetFilterExpression(this.textBox2.text, "LastName");

        if (!this.textBox1.text)
        {
            string sFirstNameFilter = GetFilterExpression(this.textBox1.text, "FirstName");

            if (!String.IsNullOrEmpty(sFirstNameFilter))
                sFilter += String.IsNullOrEmpty(sFilter) ? "" : " AND " + sFirstNameFilter;
        }

        this.filtro.RowFilter = sFilter;
    }

希望这有助于并将其标记为如果这可以解决您的问题。

已编辑:更新textBox2_Keyup事件以连接任何键入textBox1的过滤器表达式