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():允許使用指定細節模式視圖
-
private void gridView1_MasterRowGetLevelDefaultView(object sender, MasterRowGetLevelDefaultViewEventArgs e)
-
{
-
//
-
}