C1FlexGrid: Excel 样式过滤

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

发布于 2012/12/05 00:00

ComponentOne Enterprise

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

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

clip_image002

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

绑定数据

从该连接中您可以了解到如何绑定数据到C1FlexGird:http://helpcentral.componentone.com/nethelp/c1flexgrid/#!Documents/step2of3bindingc1fle.htm 。

定制过滤

定制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

关于葡萄城

葡萄城是专业的软件开发技术和低代码平台提供商,以“赋能开发者”为使命,致力于通过表格控件、低代码和BI等各类软件开发工具和服务,一站式满足开发者需求,帮助企业提升开发效率并创新开发模式。葡萄城开发技术始于1980年,40余年来始终聚焦软件开发技术,有深厚的技术积累和丰富的产品线。是业界能够同时赋能软件开发和低代码开发的企业。凭借过硬的产品能力、活跃的用户社区和丰富的伙伴生态,与超过3000家合作伙伴紧密合作,产品广泛应用于信息和软件服务、制造、交通运输、建筑、金融、能源、教育、公共管理等支柱产业。

相关产品
推荐相关案例
关注微信
葡萄城社区二维码

关注“葡萄城社区”

加微信获取技术资讯

加微信获取技术资讯

想了解更多信息,请联系我们, 随时掌握技术资源和产品动态