Filtering C1TrueDBGrid on Exact Match

ComponentOne TrueDBGrid for WinForms being a Databound control provides robust filtering options like Value Filtering, Conditional Filtering and Custom Filtering. Along with this it provides FilterBar to input the filtering text for the columns. Value Filtering imitates the filtering similar to the functionality offered by MS-Excel application. This filtering option provides a list of unique values from the column in the FilterDropDown to select the desired filtering text. However, during this filtering operation, you would observe that rows are filtered where the Filtered Column contains Cell Text starting with the Filter Text. Filtering on exact string match is not possible. This blog explains a way of Custom Filtering which provides a blend of Value Filtering along with exact string match. Binding the Grid To begin with you need to bind C1Tr**ueDBGrid to a data source. You can refer to the following Documentation link for binding C1TrueDBGrid. _Custom Filtering_ To keep the grid from automatically filtering the data you need to set the AllowFilter property to false & use the FilterChange event to apply your own custom logic. This custom implementation checks whether the filter is applied by inputting in the FilterBar or whether the user has selected values from the FilterDropdown. In case the FilterBar is used, SQL statement uses Like operator to filter the underlying data source on matching pattern. However, in case of FilterDropDown , Equal to(=) operator** is used to get the exact match filter.


int flag = 0;  
private void c1TrueDBGrid1_FilterChange(object sender, EventArgs e)  
{  
     int int_col = 0;  
     string str = null;  
     //build our filter expression  
     System.Text.StringBuilder sb = new System.Text.StringBuilder();  
     int multi = 0;  
     foreach (C1DataColumn dc in this.c1TrueDBGrid1.Columns)  
     {  
       if (dc.FilterText.Length > 0)  
       {  
         if (sb.Length > 0)  
         {  
           sb.Append(" AND ");  
         }  
         if (flag == 1)  
         {  
           if (dc.FilterText.Contains(dc.FilterSeparator))  
           {  
            string[] words = dc.FilterText.ToString().Split(dc.FilterSeparator);  
            foreach (string word in words)  
            {  
              sb.Append((dc.DataField + " = " + "'" + word + "'"));  
              sb.Append(" OR ");  
            }  
             multi = 1;  
          }  
          else  
          {  
            //using '=' in case of filter dropdown  
            sb.Append((dc.DataField + " = " + "'" + dc.FilterText + "'"));  
          }  
       }  
       else  
       {  
        if (dc.DataType.Name == "Int32")  
        {  
          int_col = 1;  
          //using 'IN' in case of filter text entered in filterbar is Integer & multiple selections are there  
          string[] words = dc.FilterText.ToString().Split(dc.FilterSeparator);  
          sb.Append(dc.DataField + " IN " + "(");  
          foreach (string word in words)  
          {  
           if (word != null)  
          {  
           sb.Append(word + ",");  
          }  
        }  
         str = sb.ToString().Remove(sb.ToString().Length - 1);  
         str += ")";  
       }  
       else  
       {  
        if (dc.FilterText.Contains(dc.FilterSeparator))  
        {  
         string[] words = dc.FilterText.ToString().Split(dc.FilterSeparator);  
         foreach (string word in words)  
         {  
          sb.Append((dc.DataField + " = " + "'" + word + "'"));  
          sb.Append(" OR ");  
         }  
          multi = 1;  
        }  
        else  
        {  
         //using 'like' in case of filter text entered in filterbar  
         sb.Append((dc.DataField + " like " + "'" + dc.FilterText + "*'"));  
        }  
       }  
      }  
     }  
    }  
    // if multiple selections are there in the dropdown or in the FilterBar  
    if (multi == 1)  
    {  
     // filter the data  
     dv.RowFilter = sb.ToString().Remove(sb.Length - 3).ToString();  
     //removing the last appended OR from sb  
    }  
    else  
    {  
     // if integer column with multiple options in filterbar  
     if (int_col == 1)  
     {  
      //filter the data using str  
      dv.RowFilter = str;  
     }  
     else  
     {  
      //filter the data using sb in integer column with a single option  
      dv.RowFilter = sb.ToString();  
     }  
    }  
    //after filtering, set the value of flag again to default  
    flag = 0;  
}  

private void c1TrueDBGrid1_FilterButtonClick(object sender, ColEventArgs e)  
{  
     //if the filter button is clicked i.e., filtering the column through the filter dropdown  
     flag = 1;  
}  

Final filtering output appears as shown in the given image: Refer to the attached samples for the detailed implementation : Download C# Sample Download VB Sample

GrapeCity

GrapeCity Developer Tools
comments powered by Disqus