[WinForm]DataGridView列头右键菜单
前言
继续"不误正业" - - #,记录一下。有时候有这样的需求:DataGridView的列头菜单可以选择具体显示哪些列,而数据区域的菜单显示增删改查,这样就需要设置两个菜单了,而DataGridView并没有为列头设置菜单的选项,这里分享一段代码。
声明
欢迎转载,但请保留文章原始出处:)
博客园:http://www.cnblogs.com
农民伯伯: http://over140.cnblogs.com
正文
一、拖拽一个ContextMenuStrip到界面上,设置好列头要显示的菜单,可以通过编程来动态设置。
二、为DataGridView设置ColumnHeaderMouseClick事件:
private void DataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
Point point = DataGridView1.PointToScreen(new Point(0, 0));
int x = 0;
DataGridViewColumnCollection columns = DataGridView1.Columns;
for (int i = 0; i < e.ColumnIndex; i++)
{
if (columns[i].Visible)
x += columns[i].Width;
}
menu1.Show(DataGridView1.PointToScreen(new Point(x + e.X, e.Y)));
}
{
if (e.Button == MouseButtons.Right)
{
Point point = DataGridView1.PointToScreen(new Point(0, 0));
int x = 0;
DataGridViewColumnCollection columns = DataGridView1.Columns;
for (int i = 0; i < e.ColumnIndex; i++)
{
if (columns[i].Visible)
x += columns[i].Width;
}
menu1.Show(DataGridView1.PointToScreen(new Point(x + e.X, e.Y)));
}
}
代码说明:
1. 只处理右键事件
2. 处理隐藏列的问题和菜单显示位置。
三、效果图:
四、后期维护
2012-5-1 如果同时设置了DataGridView的ContextMenuStrip,上面的代码将不执行,可使用下列代码:
/// <summary>
/// 显示菜单
/// </summary>
/// <param name="grid"></param>
/// <param name="menu"></param>
/// <param name="e"></param>
private void ShowMenu(DataGridView grid, ContextMenuStrip menu, DataGridViewCellMouseEventArgs e)
{
Point point = grid.PointToScreen(new Point(0, 0));
int x = 0, y = 0;
foreach (DataGridViewColumn column in grid.Columns)
{
if (column.Index >= e.ColumnIndex)
break;
if (column.Visible)
x += column.Width;
}
foreach (DataGridViewRow row in grid.Rows)
{
if (row.Index > e.RowIndex)
break;
if (row.Visible)
y += row.Height;
}
menu.Show(grid.PointToScreen(new Point(x + e.X, y + e.Y)));
/// 显示菜单
/// </summary>
/// <param name="grid"></param>
/// <param name="menu"></param>
/// <param name="e"></param>
private void ShowMenu(DataGridView grid, ContextMenuStrip menu, DataGridViewCellMouseEventArgs e)
{
Point point = grid.PointToScreen(new Point(0, 0));
int x = 0, y = 0;
foreach (DataGridViewColumn column in grid.Columns)
{
if (column.Index >= e.ColumnIndex)
break;
if (column.Visible)
x += column.Width;
}
foreach (DataGridViewRow row in grid.Rows)
{
if (row.Index > e.RowIndex)
break;
if (row.Visible)
y += row.Height;
}
menu.Show(grid.PointToScreen(new Point(x + e.X, y + e.Y)));
}
调用代码:
/// <summary>
/// 右键列头
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
ShowMenu(DataGridView1, headMenu, e);
}
}
/// <summary>
/// 右键内容区域
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
ShowMenu(DataGridView1, MenuRemove, e);
}
/// 右键列头
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
ShowMenu(DataGridView1, headMenu, e);
}
}
/// <summary>
/// 右键内容区域
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
ShowMenu(DataGridView1, MenuRemove, e);
}
}
代码说明:
这样就不要再设置DataGridView的ContextMenuStrip,自己来控制到底显示哪个菜单和菜单的位置,如此可更加灵活显示多个菜单。