EndangeringSpecies
EndangeringSpecies

Reputation: 1594

is there a convenient way to filter by a group column in XtraGrid GridView without displaying that column?

this is a continuation of my previous question is there off-the-shelf convenient way to filter by a group column in XtraGrid GridView? .

I was told to set GridView.OptionsView.ShowGroupedColumns to true, but that simply sucks. I don't want to waste horizontal space in my grid showing the group columns - it is sufficient to show them as group headers. I only want to have nice filter textboxes for those columns on top.

Any other suggestions short of rolling my own?

Upvotes: 0

Views: 2760

Answers (1)

DevExpress Team
DevExpress Team

Reputation: 11376

XtraGrid cannot show the auto filter editor behind the group column if the corresponding column is not shown in the column header panel. A possible solution is to show an external editor near the grid. One more solution - is to show an editor at the position of the group column header. In this case, you will have to implement filtering and managing the editor yourself. Something like this:

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));
            }
            ));
        }

Upvotes: 1

Related Questions