设置选中行的背景色、而不改变前景色。
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 = ;
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;
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 == )
{
itemOpen.Enabled = true;
itemDelete.Enabled = true;
}
else if(select > )
{
itemDelete.Enabled =true;
}
}
实现拖拽多选
GridView可以通过Shift键或Ctrl键以及Ctrl+A快捷键实现多选,但是默认不支持拖拽多选,好像也没有设置的方法。这样虽然没什么问题,但是肯定会给用户带来不便。
首先要设置OptionsSelection中的MultiSelect为true,也就是允许多选,否则下面的一切都是浮云。
本文通过以下代码实现拖拉多选的功能,主要是编写MouseDown、MouseMove、MouseUp三个函数。
这里需要注意一下GridHitInfo,这个类可以根据x、y坐标获取该点在GridView中的相关信息,例如在哪行哪列哪个单元格内,或者是否在单元格里。
//用于记录,鼠标是否已按下
bool isMouseDown = false; //用于鼠标拖动多选,标记是否记录开始行
bool isSetStartRow = false; //用于鼠标拖动多选,记录开始行
private int StartRowHandle = -; //用于鼠标拖动多选,记录现在行
private int CurrentRowHandle = -; //用于实现鼠标拖动选择多行功能中的一个方法,对单元格区域进行选中
private void SelectRows(int startRow, int endRow)
{
if (startRow > - && endRow > -)
{
gridView.BeginSelection();
gridView.ClearSelection();
gridView.SelectRange(startRow, endRow);
gridView.EndSelection();
}
} //实现鼠标拖动选择多行 ,鼠标按下事件
private void gridView_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isMouseDown = true;
}
} //实现鼠标拖动选择多行 ,鼠标移动时
private void gridView_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseDown)
{
GridHitInfo info = gridView.CalcHitInfo(e.X, e.Y);
//如果鼠标落在单元格里
if (info.InRow)
{
if (!isSetStartRow)
{
StartRowHandle = info.RowHandle;
isSetStartRow = true;
}
else
{
//获得当前的单元格
int newRowHandle = info.RowHandle;
if (CurrentRowHandle != newRowHandle)
{
CurrentRowHandle = newRowHandle;
//选定 区域 单元格
SelectRows( StartRowHandle, CurrentRowHandle);
}
}
}
}
} //实现鼠标拖动选择多行 ,鼠标放开时
private void gridView_MouseUp(object sender, MouseEventArgs e)
{
StartRowHandle = -;
CurrentRowHandle = -;
isMouseDown = false;
isSetStartRow = false;
}
修改列的背景色
DevExpress.XtraGrid.StyleFormatCondition cn;
cn = new DevExpress.XtraGrid.StyleFormatCondition(DevExpress.XtraGrid.FormatConditionEnum.Equal, GridView1.Columns["列名"], null, );
cn.Appearance.BackColor = Color.Red;
GridView1.FormatConditions.Add(cn);
cn = new DevExpress.XtraGrid.StyleFormatCondition(DevExpress.XtraGrid.FormatConditionEnum.Equal, GridView1.Columns["列名"], null, );
cn.Appearance.BackColor = Color.Green;
GridView1.FormatConditions.Add(cn);
这样,这列如果值为0则是红色,为1则是绿色
去除”Drag a Column Header Here To Group by that Column”
Views-OptionsView-ShowGroupPanel=False
禁用GridControl中列头的过滤器
过滤器如下图所示:
设置 Run Design->OptionsCustomization->AllowFilter 设置为:false
显示水平滚动条?
设置this.gridView.OptionsView.ColumnAutoWidth = false;
设置成一次选择一行,并且不能被编辑
this.gridView1.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFocus;
this.gridView1.OptionsBehavior.Editable = false;
this.gridView1.OptionsSelection.EnableAppearanceFocusedCell = false;
显示行号?
this.gridView1.IndicatorWidth = ;
//显示行的序号
private void gridView1_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
{
if (e.Info.IsRowIndicator && e.RowHandle>=)
{
e.Info.DisplayText = (e.RowHandle + ).ToString();
}
}
让各列头禁止移动?
设置gridView1.OptionsCustomization.AllowColumnMoving = false;
让各列头禁止排序?
设置gridView1.OptionsCustomization.AllowSort = false;
禁止各列头改变列宽?
设置gridView1.OptionsCustomization.AllowColumnResizing = false;
设置单元格自动换行?
1.ColumnEdit 增加 MemoEdit
2.AppearanceCell-TextOptions- WordWrap
3.OptionsView RowAutoHeight
设置每一列对齐方式?
View-Appearance-Row-TextOptions-(HAlignment = Center, VAlignment = Center)
设置行只读属性?
GridView.OptionsBehavior-Editable(false)