Skip to main content Skip to footer

C1TrueDBGrid: Row Filter in InvertedView

ComponentOne TrueDBGrid for WinForms provides an option to view data in different ways through the DataView property. This property supports Hierarchical, Drop-down Hierarchical, Form, Inverted, and multiple line views. Along with this, C1TrueDbGrid also provides the option to filter the data rows with the help of FilterBar available under the Column Headers. However, when the DataView is Inverted, the FilterBar is not visible & it is not possible to implement record-set filtering directly in the Grid. In this blog, we'll discuss a possible solution which implements customized row filtering in C1TrueDBGrid when the DataView is Inverted. To begin with you need to bind C1TrueDBGrid to a datasource. You can refer to the following Documentation link for binding C1TrueDBGrid. Once the grid is bound, set the DataView property to Inverted. Since, the FilterBar is not visible in this case, you would need to add TextBoxes to the Form which substitutes the place of FilterBar. Add a TextBox for each Column in the C1TrueDBGrid control. You can setup the TextBoxes either in designer or you can do it through code. Once you are finished setting up the layout, you can now apply Custom Filtering in the TextChanged event of these textboxes. Custom Filtering creates a SQL command which is used to filter the underlying DataSource.

private void tb_TextChanged(object sender,EventArgs e)  
{  
   string filter_text = ((TextBox)sender).Text;  
   int index;  
   // build our filter expression  
   StringBuilder sb = new StringBuilder();  
   foreach (Control c in splitter1.Controls)  
   {  
     if (((TextBox)sender) == c)  
     {  
       index = splitter1.Controls.IndexOf(c);  
       foreach (C1.Win.C1TrueDBGrid.C1DataColumn dc in this.c1TrueDBGrid1.Columns)  
       {  
         if (filter_text.Length > 0 && dc.DataField == splitter1.Controls[index - 1].Text)  
         {  
           if (sb.Length > 0) sb.Append(" AND ");  
           if (dc.DataType == Type.GetType("System.Int32"))  
            sb.Append(dc.DataField + " = " + filter_text );  
           else  
            sb.Append(dc.DataField + " like " + "'" + filter_text + "*'");  
         }  
       }  
    }  
  }  

  // filter the data in the Grid  
  c1NWINDDataSet.Employees.DefaultView.RowFilter = sb.ToString();  
}  

The final output would look as shown in the bottom image: Refer to the attached samples for the detailed implementation : Download C# Sample Download VB Sample

MESCIUS inc.

comments powered by Disqus