引用自:http://blog.csdn.net/mask_soft/article/details/8985454
GridView右键菜单
一、添加右键菜单
1.在VS工具箱中的“菜单和工具栏”找到ContextMenuStrip控件,双击添加。
2.点击ContextMenuStrip右上方的小三角形,打开编辑项,可以添加菜单项。至于菜单点击事件,这里就不多说了。
3.选择gridControl(注意这里不是gridView的属性),在属性中可以找到ContextMenuStrip属性,设置成刚添加的ContextMenuStrip。
这样的话,运行起来右击表格就可以看到右键菜单了。
二、是否可用设置
在不同情况下,例如选中行的个数以及内容的不同,右键菜单的菜单项是否可用需要作出判断,
这里需要用到gridView的PopupMenuShowing这个事件。也就是在菜单出现之前用户点击右键之后,来判断一下选择了几行,从而决定菜单项是否可用。
- private void gridView_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
- {
- //获取选择的行数
- int select = gridView.SelectedRowsCount;
- itemOpen.Enabled = false;
- itemDelete.Enabled = false;
- if(select == 1)
- {
- itemOpen.Enabled = true;
- itemDelete.Enabled = true;
- }
- else if(select > 1)
- {
- itemDelete.Enabled =true;
- }
- }
设置选中行的背景色、而不改变前景色。
- EnableAppearanceFocusedCell = False, EnableAppearanceFocusedRow = False
- private void gdvMarket_RowCellStyle(object sender, RowCellStyleEventArgs e)
- {
- if (e.RowHandle == gdvMarket.FocusedRowHandle)
- {
- e.Appearance.BackColor=Color.CadetBlue;
- ;
- }
- }
-
单元格颜色的设置。
- //最低价颜色控制
- DevExpress.XtraGrid.StyleFormatCondition lowPrice = new DevExpress.XtraGrid.StyleFormatCondition();
- lowPrice.Column = LowPrice;
- lowPrice.Appearance.ForeColor = Color.Red;
- lowPrice.Appearance.Options.UseForeColor = true;
- lowPrice.Condition = DevExpress.XtraGrid.FormatConditionEnum.Expression;
- lowPrice.Expression = "[LowPrice] > [PrevPrice]";
- this.gdvMarket.FormatConditions.Add(lowPrice);
- //涨跌颜色控制
- DevExpress.XtraGrid.StyleFormatCondition range = new DevExpress.XtraGrid.StyleFormatCondition();
- range.Column = Range;
- range.Appearance.ForeColor = Color.Red;
- range.Appearance.Options.UseForeColor = true;
- range.Condition = DevExpress.XtraGrid.FormatConditionEnum.Greater;
- range.Value1 = 0;
- this.gdvMarket.FormatConditions.Add(range);
-
单元格字符格式化方式
- this.gdvMarket.Columns["RangePercent"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
- this.gdvMarket.Columns["RangePercent"].DisplayFormat.FormatString = "{0}%";
-
设置列背景色
- this.gdvMarket.Columns["Amount"].AppearanceCell.BackColor = Color.AliceBlue;
- this.gdvMarket.Columns["Amount"].AppearanceCell.Options.UseBackColor = true;
-