这是我上一个问题is there off-the-shelf convenient way to filter by a group column in XtraGrid GridView?的延续。
我被告知将GridView.OptionsView.ShowGroupedColumns设置为true,但这简直太糟糕了。我不想在显示组列的网格中浪费水平空间 - 将它们显示为组标题就足够了。我只想为这些列提供漂亮的过滤器文本框。
还有其他建议吗?
答案 0 :(得分: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));
}
));
}