Dev中GridView——事件
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 = idView1.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)//点击的单元格所属列的信息
{ }
}
gridview不显示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)
{
//
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论