需要有关过滤DataGrid的建议

我正在使用.NET 3.5,C#和WinForms。

我的网格有很多列:SellerName,BuyerName,LoadType,LoadName,DriverName,CarSerialNumber等。我想过滤BindingSource。 我使用ComboBoxes做了这个,它在DropDown上填充了网格单元格的值,但它不实用,并且造成了一个看起来很糟糕的forms。

我需要建议什么是让用户选择网格值然后用按钮过滤的最佳方法。 我可以像在Excel中一样制作吗? 列标题上有一个按钮,当用户按下它时,它会显示一个带有选中列表框的小菜单。 当用户检查任何值并按下按钮时,它开始过滤。

请告诉我一些事情。

这是Excel的图片:

截图

谢谢!

好,
首先,您应该创建自定义filterusercontrol,与您在excel中的filter一样。

其次,这并不容易,但您可以向网格添加filter按钮(只需执行grid.Controls.Add(...) )并通过覆盖DatagridView OnColumnWidthChanged/OnColumnHeadersHeightChanged使它们与列的标题保持对齐。

最后,当用户点击filter按钮时,您可以打开一个ToolStripDropDown其中嵌入了自定义filter,我的意思是类似于这个答案(显然是使用您的控件而不是listview):
在.NET中使用ScrollBar的DropDown菜单


编辑:

这是一个(工作)代码示例:

自定义列标题单元格类:

 public class DataGridFilterHeader : DataGridViewColumnHeaderCell { PushButtonState currentState = PushButtonState.Normal; Point cellLocation; Rectangle buttonRect; public event EventHandler FilterButtonClicked; protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates dataGridViewElementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts) { base.Paint(graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts); int width = 20; // 20 px buttonRect = new Rectangle(cellBounds.X + cellBounds.Width - width, cellBounds.Y, width, cellBounds.Height); cellLocation = cellBounds.Location; // to set image/ or some other properties to the filter button look at DrawButton overloads ButtonRenderer.DrawButton(graphics, buttonRect, "F", this.DataGridView.Font, false, currentState); } protected override void OnMouseDown(DataGridViewCellMouseEventArgs e) { if (this.IsMouseOverButton(e.Location)) currentState = PushButtonState.Pressed; base.OnMouseDown(e); } protected override void OnMouseUp(DataGridViewCellMouseEventArgs e) { if (this.IsMouseOverButton(e.Location)) { currentState = PushButtonState.Normal; this.OnFilterButtonClicked(); } base.OnMouseUp(e); } private bool IsMouseOverButton(Point e) { Point p = new Point(eX + cellLocation.X, eY + cellLocation.Y); if (pX >= buttonRect.X && pX <= buttonRect.X + buttonRect.Width && pY >= buttonRect.Y && pY <= buttonRect.Y + buttonRect.Height) { return true; } return false; } protected virtual void OnFilterButtonClicked() { if (this.FilterButtonClicked != null) this.FilterButtonClicked(this, new ColumnFilterClickedEventArg(this.ColumnIndex, this.buttonRect)); } } 

自定义事件参数:

 public class ColumnFilterClickedEventArg : EventArgs { public int ColumnIndex { get; private set; } public Rectangle ButtonRectangle { get; private set; } public ColumnFilterClickedEventArg(int colIndex, Rectangle btnRect) { this.ColumnIndex = colIndex; this.ButtonRectangle = btnRect; } } 

DataGridView覆盖:

 public class DataGridWithFilter : DataGridView { protected override void OnColumnAdded(DataGridViewColumnEventArgs e) { var header = new DataGridFilterHeader(); header.FilterButtonClicked += new EventHandler(header_FilterButtonClicked); e.Column.HeaderCell = header; base.OnColumnAdded(e); } void header_FilterButtonClicked(object sender, ColumnFilterClickedEventArg e) { // open a popup on the bottom-left corner of the // filter button // here's we add a simple hello world textbox, but you should add your filter control TextBox innerCtrl = new TextBox(); innerCtrl.Text = "Hello World !"; innerCtrl.Size = new System.Drawing.Size(100, 30); var popup = new ToolStripDropDown(); popup.AutoSize = false; popup.Margin = Padding.Empty; popup.Padding = Padding.Empty; ToolStripControlHost host = new ToolStripControlHost(innerCtrl); host.Margin = Padding.Empty; host.Padding = Padding.Empty; host.AutoSize = false; host.Size = innerCtrl.Size; popup.Size = innerCtrl.Size; popup.Items.Add(host); // show the popup popup.Show(this, e.ButtonRectangle.X, e.ButtonRectangle.Bottom); } } 

结果:

过滤图像


编辑2:

这是一个完整的VS2008项目示例(带有自定义filter的DataGrid,而不仅仅是“Hello World”): - > http://www.mediafire.com/?s6o8jmpzh0t82v2