ComponentOne FlexGrid for Winforms提供了丰富的对象模型,是您可以实现强大的功能。C1FlexGrid 用于内置的列过滤,可以通过 AllowFiltering 属性调用。当过滤功能开启时,用户可以通过点击过滤按钮编辑过滤条件。过滤编辑器语序用户根据当前该列展示的数据定制过滤条件,或者定制定制符合当前展示行的过滤条件。

默认的过滤行为是通过下拉列表来展示所有行数值,通过点击下拉列表中的checkbox来显示或隐藏当前值对应的所有行。

clip_image002

在本篇文章中,我们将阐述如何实现Excel 样式的过滤。即为,当前列应用过滤后,过滤下拉列表再次展开时只显示过滤后显示的行数值。

绑定数据

从该连接中您可以了解到如何绑定数据到C1FlexGird:Documentation link

定制过滤

定制Grid的过滤行为,我们需要操作过滤编辑器的按钮点击事件。可以通过, 需要捕获用于应用额取消过滤的FilterEditorForm。基本实现逻辑为当过滤应用到当前列时重置当前 C1FlexGrid 的数据源。

private void c1FlexGrid1_MouseDown(object sender, MouseEventArgs e)

{

    if (c1FlexGrid1.HitTest(e.X,e.Y).Type == C1.Win.C1FlexGrid.HitTestTypeEnum.FilterIcon)

    {

       // start the timer if the FilterIcon has been hit

       tm.Start();

    }

}

void tm_Tick(object sender, EventArgs e)

{

    tm.Stop();

    foreach (Form f in Application.OpenForms)

    {

       if (f.Name == "FilterEditorForm" && f.GetType().ToString() == "C1.Win.C1FlexGrid.FilterEditorForm")

       {

           // Capture the Apply & Clear buttons on the filterdropdown form & assign Handlers to their Click Events

           System.Windows.Forms.ToolStripButton clr_btn = ((ToolStripButton)((ToolStrip)f.Controls[0]).Items[1]);

           System.Windows.Forms.ToolStripButton app_btn = ((ToolStripButton)((ToolStrip)f.Controls[0]).Items[2]);

           clr_btn.Click += new EventHandler(clr_btn_Click);

           app_btn.Click += new EventHandler(app_btn_Click);

       }

    }

}

点击 ApplyClear 按钮时。应用自定义过滤:

// Handling Apply button's Click Event

void app_btn_Click(object sender, EventArgs e)

{

    string filter_text = null;

    for (int i = 0; i < c1FlexGrid1.Cols.Count; i++)

    {

        // check for which column the filter is currently active

        if (c1FlexGrid1.Cols[ i ].Filter.IsActive == true)

        {

            object[] filter_values = ((C1.Win.C1FlexGrid.ColumnFilter)(this.c1FlexGrid1.Cols[ i ].Filter)).ValueFilter.ShowValues;

            foreach (object value in filter_values)

            {

               // if the Column is Integer

               if (c1FlexGrid1.Cols[ i ].DataType.ToString() == "System.Int32")

               {

                  // check for null values in the column

                  if (value.ToString() == "")

                      filter_text += c1FlexGrid1.Cols[ i ].Caption + " Is Null or ";

                  else

                      filter_text += c1FlexGrid1.Cols[ i ].Caption + "=" + value + " or ";

               }

               else // columns with Datatype not as Integer

               {

                  // check for null values in the column

                  if (value.ToString() == "")

                     filter_text += c1FlexGrid1.Cols[ i ].Caption + " Is Null or ";

                  else

                     filter_text += c1FlexGrid1.Cols[ i ].Caption + " like '" + value + "' or ";

               }

            }

            dv.RowFilter = filter_text.Remove(filter_text.Length - 3);    // removing the extra appended or from the filter_text

            // Assign the filtered datasource to the Grid

            c1FlexGrid1.DataSource = dv;

         }

     }

}

// Handling Clear button's Click Event

void clr_btn_Click(object sender, EventArgs e)

{

     // Assign the default datasource to the Grid

     c1FlexGrid1.DataSource = c1NWINDDataSet.Employees;

}

AfterFiltering

Demo 下载 :
Download C# Sample
Download VB Sample