DevExpress自定义XtraGrid列菜单
先看下面的图
1.自定义的列菜单
2.XtraGrid自带的列菜单
3.混合的列菜单
实现代码:
代码
private void gvUploadData_ShowGridMenu(object sender, GridMenuEventArgs e)
{
if (e.MenuType == DevExpress.XtraGrid.Views.Grid.GridMenuType.Column)
{
DevExpress.XtraGrid.Menu.GridViewColumnMenu menu = e.Menu as GridViewColumnMenu;
//清空自带的菜单项
//menu.Items.Clear();
if (menu.Column != null)
{
// 添加自定义的菜单项
menu.Items.Add(CreateMenuItem("编辑当前列", menu.Column, icList.Images[1]));
menu.Items.Add(CreateMenuItem("锁定当前列", menu.Column, icList.Images[2]));
menu.Items.Add(CreateMenuItem("清空当前列", menu.Column, icList.Images[3]));
}
}
}
DXMenuItem CreateMenuItem(string caption, GridColumn column, Image image)
{
DXMenuItem item = new DXMenuItem(caption, new EventHandler(OnFixedClick), image);
item.Tag = new MenuInfo(column);
return item;
}
void OnFixedClick(object sender, EventArgs e)
{
DXMenuItem item = sender as DXMenuItem;
MenuInfo info = item.Tag as MenuInfo;
if (info == null) return;
//string s = info.Column.FieldName;
//在这里进行业务处理
}
{
if (e.MenuType == DevExpress.XtraGrid.Views.Grid.GridMenuType.Column)
{
DevExpress.XtraGrid.Menu.GridViewColumnMenu menu = e.Menu as GridViewColumnMenu;
//清空自带的菜单项
//menu.Items.Clear();
if (menu.Column != null)
{
// 添加自定义的菜单项
menu.Items.Add(CreateMenuItem("编辑当前列", menu.Column, icList.Images[1]));
menu.Items.Add(CreateMenuItem("锁定当前列", menu.Column, icList.Images[2]));
menu.Items.Add(CreateMenuItem("清空当前列", menu.Column, icList.Images[3]));
}
}
}
DXMenuItem CreateMenuItem(string caption, GridColumn column, Image image)
{
DXMenuItem item = new DXMenuItem(caption, new EventHandler(OnFixedClick), image);
item.Tag = new MenuInfo(column);
return item;
}
void OnFixedClick(object sender, EventArgs e)
{
DXMenuItem item = sender as DXMenuItem;
MenuInfo info = item.Tag as MenuInfo;
if (info == null) return;
//string s = info.Column.FieldName;
//在这里进行业务处理
}
还有一个类
class MenuInfo
{
public GridColumn Column;
public MenuInfo(GridColumn column)
{
this.Column = column;
}
}
参考了以下内容:
http://www.devexpress.com/Help/?document=XtraGrid/CustomDocument709.htm
http://www.devexpress.com/Help/?document=XtraGrid/CustomDocument3505.htm&levelup=true