日期:2014-05-20  浏览次数:21229 次

WinForm DataGridView控件如何做到Excel这种筛选? [图]
VS2005,WinForm,C#,SQL2000



请问:
VS2005自带的DataGridView控件能否做到 类似Excel的这种筛选功能?

或者提供下其它的方法,思路也可以;

[谢绝UP者,谢绝帮顶者,谢绝路过者]

------解决方案--------------------
为每一列group后的值动态生成下拉列表框
------解决方案--------------------
我提供个思路,也就是DataGridView空间中加了list 的功能
自己编写一个类,继承DataGridView,左键点击格网空白处时,弹出下拉列表进行功能设计
------解决方案--------------------
发段代码上来 希望对你有帮助!!

[code=C#][/code]/// <summary>
/// 触发CellPainting事件
/// </summary>
/// <param name="e"></param>
protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
{
#region 画列头下拉框

if (isColumnHeaderFilter && e.RowIndex == -1 && e.ColumnIndex > -1)
{
string columnName = Columns[e.ColumnIndex].Name;
bool find = false;
foreach (string name in filterColumnNames)
{
if (string.Compare(name, columnName, true) == 0)
{
find = true;
break;
}
}
if (find)
{
e.PaintBackground(e.ClipBounds, false);
e.PaintContent(e.ClipBounds);
e.Paint(e.ClipBounds, DataGridViewPaintParts.SelectionBackground | DataGridViewPaintParts.Focus |
DataGridViewPaintParts.ErrorIcon | DataGridViewPaintParts.ContentBackground);
// 画按钮
Rectangle rect = GetColumnHeaderDropDownBounds(e.CellBounds);
e.Graphics.FillRectangle(SystemBrushes.ButtonFace, rect);
Point pLT = rect.Location;
Point pRT = new Point(rect.Right, rect.Top);
Point pLB = new Point(rect.Left, rect.Bottom);
Point pRB = new Point(rect.Right, rect.Bottom);
e.Graphics.DrawLine(SystemPens.ButtonHighlight, pLT, pRT);
e.Graphics.DrawLine(SystemPens.ButtonHighlight, pLT, pLB);
e.Graphics.DrawLine(SystemPens.ButtonShadow, pRT, pRB);
e.Graphics.DrawLine(SystemPens.ButtonShadow, pLB, pRB);
// 画三角形
Point p31 = new Point(pLT.X + (pRT.X - pLT.X) / 4, pLT.Y + (pLB.Y - pLT.Y) / 4);
Point p32 = new Point(pRT.X - (pRT.X - pLT.X) / 4, pLT.Y + (pLB.Y - pLT.Y) / 4);
Point p33 = new Point(pLT.X + (pRT.X - pLT.X) / 2, pLB.Y - (pLB.Y - pLT.Y) / 4);
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
int[] columnList = new int[columnHeaderFilter.Keys.Count];
columnHeaderFilter.Keys.CopyTo(columnList, 0);
if (Array.IndexOf(columnList, e.ColumnIndex) == -1)
{
e.Graphics.FillPolygon(SystemBrushes.ControlText, new Point[] { p31, p32, p33, p31 });
}
else
{
e.Graphics.FillPolygon(SystemBrushes.ControlLightLight, new Point[] { p31, p32, p33, p31 });
}
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
e.Handled = true;
}
}

#endregion

base.OnCellPainting(e);
}
[code=C#][/code]
------解决方案--------------------