gridControl下的事件一般是包含View切换,点击,更改事件,用的不多。而每一个View下的事件我们却常用到。
GridView中事件:
GridView中大多数事件都会用到e这个参数,从e这个参数中我们可以获取很多信息。e是根据事件来定义级别的,可以获取e的层级以上的信息,但不能获取e的层及以下的信息。
1、CustomDrawEmptyForeground(自定义绘制空白前景):当没有显示任何行时,允许对视图的空间进行自定义绘制。
private void gridView1_CustomDrawEmptyForeground(object sender, DevExpress.XtraGrid.Views.Base.CustomDrawEventArgs e)
{
string txt = "空白!";
Font font = new Font("宋体", 20, FontStyle.Bold);
e.Graphics.DrawString(txt, font, Brushes.Purple, e.Bounds.Top + 200, e.Bounds.Left + 200);
//BindingSource bindingsource = this.gridView1.DataSource as BindingSource; //封装窗体的数据源
//if (bindingsource == null)
//{
// string txt = "空白!";
// Font font = new Font("宋体", 20, FontStyle.Bold);
// //存储一组整数,共四个,表示一个矩形的位置和大小。Rectangle(int x, int y, int width, int height);
// //参数为:矩形左上角的 x 坐标。矩形左上角的 y 坐标。矩形的宽度。矩形的高度。
// Rectangle r = new Rectangle(e.Bounds.Top+200, e.Bounds.Left+200,e.Bounds.Right, e.Bounds.Height);
// e.Graphics.DrawString(txt, font, Brushes.Purple, r);
//}
}
2、CellMerge(单元格合并):提供自定义单元合并行为的功能。需先设置gridView1.OptionsView.AllowCellMerge = true;
private void gridView1_CellMerge(object sender, DevExpress.XtraGrid.Views.Grid.CellMergeEventArgs e)
{
if (e.Column.FieldName != "Name")
e.Handled = true; //获取或设置是否处理单元格合并操作,因此不需要进行默认处理。
}
3、CustomColumnDisplayText(自定义列显示):为数据单元格内的值、组行和过滤下拉菜单自定义显示文本
gridControl的每一列原始数据是Value,但是显示数据是DisplayText,默认DisplayText的值即是Value通过DisplayFormat转换后的值。
private void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
{
if (e.Column.FieldName == "Data")
{
string a = Convert.ToInt16(e.Value) < 0 ? "负数" : "正数"; //是否为正数
switch(a)
{
case "负数":
e.DisplayText = "负数据";
break;
case"正数":
e.DisplayText = "正数据";
break;
}
}
}
4、CustomDrawGroupRow(自定义绘制组行):允许手动绘制组行
private void gridView1_CustomDrawGroupRow(object sender, DevExpress.XtraGrid.Views.Base.RowObjectCustomDrawEventArgs e)
{
GridGroupRowInfo gridGroupRowInfo = e.Info as GridGroupRowInfo;
gridGroupRowInfo.GroupText = "第" + (e.RowHandle) + "行" + gridGroupRowInfo.EditValue;
}
5、CustomDrawRowIndicator(自定义行号显示):能够自定义绘制行指示器面板中的元素。需先设置行指示面板宽度gridView1.IndicatorWidth = 70;
private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
{
if (e.Info.IsRowIndicator)
{
e.Info.DisplayText = "Row" + e.RowHandle;
}
}
6、RowCellClick(单元格点击事件):如果数据是可编辑的,事件将不会触发
private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
{
if (e.Button == MouseButtons.Left) //鼠标左键
{
//执行的方法
}
if (e.Clicks == 2) //双击
{ }
if (e.Delta > 0)//鼠标滚轮滚动方向
{ }
if (e.X > 0 & e.Y > 0)//鼠标的坐标
{ }
if (e.RowHandle > 0) //点击的行号
{ }
if (e.CellValue != null)//点击的单元格中的值
{ }
if (e.Column != null)//点击的单元格所属列的信息
{ }
}
7、RowClick(行点击事件):如果点击数据是可编辑的,事件将不会触发
private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
{
if (e.Clicks == 2) //双击
{ }
}
8、CustomDrawCell(重绘列样式):自定义绘制数据单元格
private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
{
if (e.Column.Caption == "数据")
{
GridCellInfo gridCellInfo = e.Cell as GridCellInfo;
if (gridCellInfo.IsDataCell && double.Parse(gridCellInfo.CellValue.ToString()) < 0)
{
e.Appearance.BackColor = Color.Yellow;
}
else { e.Appearance.BackColor = Color.Green; }
}
}
9、CalcPreviewText(自定义备注):自定义备注文本 需先设置gridView1.OptionsView.ShowPreview = true;
private void gridView1_CalcPreviewText(object sender, DevExpress.XtraGrid.Views.Grid.CalcPreviewTextEventArgs e)
{
DataRow dr = gridView1.GetDataRow(e.RowHandle);
e.PreviewText = dr["Name"] + ":" + dr["Sex"];
}
10、RowCellStyle(定制单元格外观):允许单个单元格的外观设置得以更改
private void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
{
//GridView View = sender as GridView;
if (e.Column.FieldName == "Age" || e.Column.FieldName == "Data")
{
//string category = View.GetRowCellDisplayText(e.RowHandle, View.Columns["Category"]);
string category = gridView1.GetRowCellDisplayText(e.RowHandle, gridView1.Columns["Name"]);
if (category == "张三")
{
e.Appearance.BackColor = Color.DeepSkyBlue;
e.Appearance.BackColor2 = Color.LightCyan;
}
}
}
11、RowStyle(定制行外观):允许更改各行的外观设置
private void gridView1_RowStyle(object sender, RowStyleEventArgs e)
{
GridView View = sender as GridView;
if (e.RowHandle >= 0)
{
string category = View.GetRowCellDisplayText(e.RowHandle, View.Columns["Name"]);
if (category == "张三")
{
e.Appearance.BackColor = Color.DeepSkyBlue;
e.Appearance.BackColor2 = Color.SeaShell;
}
}
}
12、MasterRowGetRelationCount(主行获取关系数目):接管此事件来为每个主控行指定主/从关系的数目
private void gridView1_MasterRowGetRelationCount(object sender, MasterRowGetRelationCountEventArgs e)
{
e.RelationCount = 1; //显示1个关系,若设置为非正数则展开按钮被隐藏
}
13、MasterRowEmpty(指定当前细节视图是否有数据):允许指定细节是否为空
private void gridView1_MasterRowEmpty(object sender, MasterRowEmptyEventArgs e)
{
int a = e.RowHandle;//获取主控行的 句柄 由事件的 RowHandle 参数标识
int b = e.RelationIndex; //获取引用当前细节数据的 RelationIndex 参数
e.IsEmpty = false; //展示数据
}
14、MasterRowGetChildList(接管此事件来为当前细节视图提供数据):允许手动加载细节数据
private void gridView1_MasterRowGetChildList(object sender, MasterRowGetChildListEventArgs e)
{
//
}
15、MasterRowGetRelationName(接管此事件为当前关系 (细节) 提供名称):允许使用指定的细节视图 。需先构建GridControl.LevelTree 树
private void gridView1_MasterRowGetRelationName(object sender, MasterRowGetRelationNameEventArgs e)
{
e.RelationName = "关系";
}
16、MasterRowGetLevelDefaultView():允许使用指定细节模式视图
接管此事件来为当前呈现的细节视图显式提供模式视图。 通常,如果所需的模式视图不属于 GridControl.LevelTree 树,则需要接管此事件。 否则,在大多数情况下,可以通过 GridView.MasterRowGetRelationName 事件间接提供模式视图。
private void gridView1_MasterRowGetLevelDefaultView(object sender, MasterRowGetLevelDefaultViewEventArgs e)
{
//
}