0

这是我之前的问题的延续是否有现成的方便的方法来按 XtraGrid GridView 中的组列进行过滤?.

我被告知将 GridView.OptionsView.ShowGroupedColumns 设置为 true,但这很糟糕。我不想在显示组列的网格中浪费水平空间 - 将它们显示为组标题就足够了。我只想为顶部的那些列提供漂亮的过滤器文本框。

除了自己动手之外,还有其他建议吗?

4

1 回答 1

1

如果相应的列未显示在列标题面板中,XtraGrid 无法在组列后面显示自动过滤器编辑器。一种可能的解决方案是在网格附近显示一个外部编辑器。另一种解决方案 - 是在组列标题的位置显示一个编辑器。在这种情况下,您必须自己实现过滤和管理编辑器。像这样的东西:

using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Drawing;
using DevExpress.Data.Filtering;


        private void gridView1_Click(object sender, EventArgs e) {
            MouseEventArgs args = e as MouseEventArgs;
            GridView view = sender as GridView;
            GridHitInfo hitInfo = view.CalcHitInfo(args.X, args.Y);
            if(hitInfo.InGroupColumn) {
                ShowFilterEditor(hitInfo.Column);
            }
        }

        private void ShowFilterEditor(GridColumn gridColumn) {
            GridView gridView = gridColumn.View as GridView;
            GridViewInfo vInfo = gridView.GetViewInfo() as GridViewInfo;
            for(int i = 0; i < vInfo.GroupPanel.Rows.Count; i++) 
                for(int j = 0; j < vInfo.GroupPanel.Rows[i].ColumnsInfo.Count; j ++) {
                    GridColumnInfoArgs columnInfo = vInfo.GroupPanel.Rows[i].ColumnsInfo[gridColumn];
                    if(columnInfo != null) {
                        Rectangle columnRect = columnInfo.CaptionRect;
                        TextEdit edit = new TextEdit();
                        gridControl1.Controls.Add(edit);
                        edit.SetBounds(columnRect.Left, columnRect.Top, columnRect.Width, columnRect.Height);
                        edit.Focus();
                        edit.KeyPress += new KeyPressEventHandler(edit_KeyPress);
                        edit.KeyDown += new KeyEventHandler(edit_KeyDown);
                        edit.Disposed += new EventHandler(edit_Disposed);
                        edit.Tag = gridColumn;
                        return;
                    }
            }
        }

        void edit_Disposed(object sender, EventArgs e) {
            TextEdit edit = sender as TextEdit;
            edit.KeyPress -= new KeyPressEventHandler(edit_KeyPress);
            edit.KeyDown -= new KeyEventHandler(edit_KeyDown);
            edit.Disposed -= new EventHandler(edit_Disposed);
            edit.Tag = null;

        }

        void edit_KeyDown(object sender, KeyEventArgs e) {
            if(e.KeyCode == Keys.Return)
                BeginInvoke(new MethodInvoker(delegate { (sender as TextEdit).Dispose(); }));
        }

        void edit_KeyPress(object sender, KeyPressEventArgs e) {
            BeginInvoke(new MethodInvoker(delegate {
                TextEdit edit = sender as TextEdit;
                if(edit.IsDisposed)
                    return;
                GridColumn column = edit.Tag as GridColumn;
                column.FilterInfo = new ColumnFilterInfo(new BinaryOperator(column.FieldName, string.Format("%{0}%",  edit.Text), BinaryOperatorType.Like));
            }
            ));
        }
于 2011-04-11T12:37:52.203 回答