简体   繁体   中英

How do multifilter in dataGridView with two textboxes?

For example when the same First Name with different Last Name to filter name and write in Last Name filter, but keeping the names filter.

Example whith a textbox:

Connection:

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)
    {

    }
}

Load 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;
}

Event:

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;
}

Here is one approach to achieve your requirement.

Create a common function to call from both the text boxes as below

    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;
    }

Call the common function from key up event of both the text boxes as shown below;

Text Box 1 with First Name filter

    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;
    }

Text Box 2 with Last Name filter

    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;
    }

Hope this helps and mark it as so if this addresses your issue.

Edited : to update the textBox2_Keyup event to concatenate any filter expression typed into textBox1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM