Skip to main content Skip to footer

Implement an AND search using FlexGridSearchPanel in C1FlexGrid

Background:

How to implement AND search using FlexGridSearchPanel in FlexGrid

Steps to Complete:

1. Performing AND search means returning only those rows which contain all the words typed in the FlexGridSearchPanel. Here is the code snippet:

private void ApplyAndFilter()
{
   string[] strArr = null;
   int count = 0;
   char[] splitchar = { ' ' };
   if (c1FlexGridSearchPanel1.Controls[0].Text.Contains(" "))
      {
        strArr = c1FlexGridSearchPanel1.Controls[0].Text.Split(splitchar);
         for (int row = 1; row < c1FlexGrid1.Rows.Count; row++)
              {
               int temp = 0;
               for (count = 0; count <= strArr.Length - 1; count++)
                   {
           	         for (int col = 1; col < c1FlexGrid1.Cols.Count; col++)
  		  {
 		    string str = c1FlexGrid1[row, col].ToString().ToLower();
                             if (str.Contains(strArr[count].ToLower()))
    		        {
                                  temp++;
    		          break;
                                  }
                           }
                      }
                 if (temp != 2)
                   {
                    c1FlexGrid1.Rows[row].Visible = false;
                    }
                }
             }
          }

2. Call above implemented method on Search Button’s Click of FlexGridSearchPanel.

var searchButton = c1FlexGridSearchPanel1.Controls[2] as ButtonBase;
searchButton.Click += SearchButton_Click;
private void SearchButton_Click(object sender, EventArgs e)
{
     ApplyAndFilter();
}

3. Call above implemented method on pressing Enter key in FlexGridSearchPanel.

    private void ComboSearch_KeyDown(object sender, KeyEventArgs e)
        {           
          if (e.KeyData == Keys.Enter)
            {
                if (c1FlexGridSearchPanel1.Controls[0].Text == "")
                {
                    for (int row = 1; row < c1FlexGrid1.Rows.Count; row++)
                    {
                        c1FlexGrid1.Rows[row].Visible = true;
                    }

                }
                else
                {
                    ApplyAndFilter();
                }
            }
        }

Prabhat Sharma