Dev中GridView——事件

 gridControl下的事件一般是包含View切換,點擊,更改事件,用的不多。而每一個View下的事件我們卻常用到。

GridView中事件:

     GridView中大多數事件都會用到e這個參數,從e這個參數中我們可以獲取很多信息。e是根據事件來定義級別的,可以獲取e的層級以上的信息,但不能獲取e的層及以下的信息。

1、CustomDrawEmptyForeground(自定義繪製空白前景):當沒有顯示任何行時,允許對視圖的空間進行自定義繪製。
  1.  
    private void gridView1_CustomDrawEmptyForeground(object sender, DevExpress.XtraGrid.Views.Base.CustomDrawEventArgs e)
  2.  
    {
  3.  
    string txt = "空白!";
  4.  
    Font font = new Font("宋體", 20, FontStyle.Bold);
  5.  
    e.Graphics.DrawString(txt, font, Brushes.Purple, e.Bounds.Top + 200, e.Bounds.Left + 200);
  6.  
    //BindingSource bindingsource = this.gridView1.DataSource as BindingSource; //封裝窗體的數據源
  7.  
    //if (bindingsource == null)
  8.  
    //{
  9.  
    // string txt = "空白!";
  10.  
    // Font font = new Font("宋體", 20, FontStyle.Bold);
  11.  
    // //存儲一組整數,共四個,表示一個矩形的位置和大小。Rectangle(int x, int y, int width, int height);
  12.  
    // //參數爲:矩形左上角的 x 座標。矩形左上角的 y 座標。矩形的寬度。矩形的高度。
  13.  
    // Rectangle r = new Rectangle(e.Bounds.Top+200, e.Bounds.Left+200,e.Bounds.Right, e.Bounds.Height);
  14.  
    // e.Graphics.DrawString(txt, font, Brushes.Purple, r);
  15.  
    //}
  16.  
    }
2、CellMerge(單元格合併):提供自定義單元合併行爲的功能。需先設置gridView1.OptionsView.AllowCellMerge = true;
  1.  
    private void gridView1_CellMerge(object sender, DevExpress.XtraGrid.Views.Grid.CellMergeEventArgs e)
  2.  
    {
  3.  
    if (e.Column.FieldName != "Name")
  4.  
    e.Handled = true; //獲取或設置是否處理單元格合併操作,因此不需要進行默認處理。
  5.  
    }
3、CustomColumnDisplayText(自定義列顯示):爲數據單元格內的值、組行和過濾下拉菜單自定義顯示文本

    gridControl的每一列原始數據是Value,但是顯示數據是DisplayText,默認DisplayText的值即是Value通過DisplayFormat轉換後的值。

  1.  
    private void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
  2.  
    {
  3.  
    if (e.Column.FieldName == "Data")
  4.  
    {
  5.  
    string a = Convert.ToInt16(e.Value) < 0 ? "負數" : "正數"; //是否爲正數
  6.  
    switch(a)
  7.  
    {
  8.  
    case "負數":
  9.  
    e.DisplayText = "負數據";
  10.  
    break;
  11.  
    case"正數":
  12.  
    e.DisplayText = "正數據";
  13.  
    break;
  14.  
    }
  15.  
    }
  16.  
    }
4、CustomDrawGroupRow(自定義繪製組行):允許手動繪製組行
  1.  
    private void gridView1_CustomDrawGroupRow(object sender, DevExpress.XtraGrid.Views.Base.RowObjectCustomDrawEventArgs e)
  2.  
    {
  3.  
    GridGroupRowInfo gridGroupRowInfo = e.Info as GridGroupRowInfo;
  4.  
    gridGroupRowInfo.GroupText = "第" + (e.RowHandle) + "行" + gridGroupRowInfo.EditValue;
  5.  
    }
5、CustomDrawRowIndicator(自定義行號顯示):能夠自定義繪製行指示器面板中的元素。需先設置行指示面板寬度gridView1.IndicatorWidth = 70; 
  1.  
    private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
  2.  
    {
  3.  
    if (e.Info.IsRowIndicator)
  4.  
    {
  5.  
    e.Info.DisplayText = "Row" + e.RowHandle;
  6.  
    }
  7.  
    }
6、RowCellClick(單元格點擊事件):如果數據是可編輯的,事件將不會觸發
  1.  
    private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
  2.  
    {
  3.  
    if (e.Button == MouseButtons.Left) //鼠標左鍵
  4.  
    {
  5.  
    //執行的方法
  6.  
    }
  7.  
    if (e.Clicks == 2) //雙擊
  8.  
    { }
  9.  
    if (e.Delta > 0)//鼠標滾輪滾動方向
  10.  
    { }
  11.  
    if (e.X > 0 & e.Y > 0)//鼠標的座標
  12.  
    { }
  13.  
    if (e.RowHandle > 0) //點擊的行號
  14.  
    { }
  15.  
    if (e.CellValue != null)//點擊的單元格中的值
  16.  
    { }
  17.  
    if (e.Column != null)//點擊的單元格所屬列的信息
  18.  
    { }
  19.  
    }
7、RowClick(行點擊事件):如果點擊數據是可編輯的,事件將不會觸發
  1.  
    private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
  2.  
    {
  3.  
    if (e.Clicks == 2) //雙擊
  4.  
    { }
  5.  
    }
8、CustomDrawCell(重繪列樣式):自定義繪製數據單元格
  1.  
    private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
  2.  
    {
  3.  
    if (e.Column.Caption == "數據")
  4.  
    {
  5.  
    GridCellInfo gridCellInfo = e.Cell as GridCellInfo;
  6.  
    if (gridCellInfo.IsDataCell && double.Parse(gridCellInfo.CellValue.ToString()) < 0)
  7.  
    {
  8.  
    e.Appearance.BackColor = Color.Yellow;
  9.  
    }
  10.  
    else { e.Appearance.BackColor = Color.Green; }
  11.  
    }
  12.  
    }
9、CalcPreviewText(自定義備註):自定義備註文本 需先設置gridView1.OptionsView.ShowPreview = true;
  1.  
    private void gridView1_CalcPreviewText(object sender, DevExpress.XtraGrid.Views.Grid.CalcPreviewTextEventArgs e)
  2.  
    {
  3.  
    DataRow dr = gridView1.GetDataRow(e.RowHandle);
  4.  
    e.PreviewText = dr["Name"] + ":" + dr["Sex"];
  5.  
    }
10、RowCellStyle(定製單元格外觀):允許單個單元格的外觀設置得以更改
  1.  
    private void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
  2.  
    {
  3.  
    //GridView View = sender as GridView;
  4.  
    if (e.Column.FieldName == "Age" || e.Column.FieldName == "Data")
  5.  
    {
  6.  
    //string category = View.GetRowCellDisplayText(e.RowHandle, View.Columns["Category"]);
  7.  
    string category = gridView1.GetRowCellDisplayText(e.RowHandle, gridView1.Columns["Name"]);
  8.  
    if (category == "張三")
  9.  
    {
  10.  
    e.Appearance.BackColor = Color.DeepSkyBlue;
  11.  
    e.Appearance.BackColor2 = Color.LightCyan;
  12.  
    }
  13.  
    }
  14.  
    }
11、RowStyle(定製行外觀):允許更改各行的外觀設置
  1.  
    private void gridView1_RowStyle(object sender, RowStyleEventArgs e)
  2.  
    {
  3.  
    GridView View = sender as GridView;
  4.  
    if (e.RowHandle >= 0)
  5.  
    {
  6.  
    string category = View.GetRowCellDisplayText(e.RowHandle, View.Columns["Name"]);
  7.  
    if (category == "張三")
  8.  
    {
  9.  
    e.Appearance.BackColor = Color.DeepSkyBlue;
  10.  
    e.Appearance.BackColor2 = Color.SeaShell;
  11.  
    }
  12.  
    }
  13.  
    }
12、MasterRowGetRelationCount(主行獲取關係數目):接管此事件來爲每個主控行指定主/從關係的數目
  1.  
    private void gridView1_MasterRowGetRelationCount(object sender, MasterRowGetRelationCountEventArgs e)
  2.  
    {
  3.  
    e.RelationCount = 1; //顯示1個關係,若設置爲非正數則展開按鈕被隱藏
  4.  
    }
13、MasterRowEmpty(指定當前細節視圖是否有數據):允許指定細節是否爲空
  1.  
    private void gridView1_MasterRowEmpty(object sender, MasterRowEmptyEventArgs e)
  2.  
    {
  3.  
    int a = e.RowHandle;//獲取主控行的 句柄 由事件的 RowHandle 參數標識
  4.  
    int b = e.RelationIndex; //獲取引用當前細節數據的 RelationIndex 參數
  5.  
    e.IsEmpty = false; //展示數據
  6.  
    }
14、MasterRowGetChildList(接管此事件來爲當前細節視圖提供數據):允許手動加載細節數據
  1.  
    private void gridView1_MasterRowGetChildList(object sender, MasterRowGetChildListEventArgs e)
  2.  
    {
  3.  
    //
  4.  
    }
15、MasterRowGetRelationName(接管此事件爲當前關係 (細節) 提供名稱):允許使用指定的細節視圖 。需先構建GridControl.LevelTree 樹
  1.  
    private void gridView1_MasterRowGetRelationName(object sender, MasterRowGetRelationNameEventArgs e)
  2.  
    {
  3.  
    e.RelationName = "關係";
  4.  
    }
16、MasterRowGetLevelDefaultView():允許使用指定細節模式視圖
     接管此事件來爲當前呈現的細節視圖顯式提供模式視圖。 通常,如果所需的模式視圖不屬於 GridControl.LevelTree 樹,則需要接管此事件。 否則,在大多數情況下,可以通過 GridView.MasterRowGetRelationName 事件間接提供模式視圖。

 

  1.  
    private void gridView1_MasterRowGetLevelDefaultView(object sender, MasterRowGetLevelDefaultViewEventArgs e)
  2.  
    {
  3.  
    //
  4.  
    }
 
 
一般屬性設置 不顯示分組框:Gridview->Option View->Show Group Panel=false 單元格不可編輯:gridcontrol -->gridview -->OptionsBehavior -->Editable=false 禁用過濾器:Run Design->OptionsCustomization->AllowFilt...
Devexpress GridView 數據格式化顯示 gridView1.CustomColumnDisplayText += gridView1_CustomColumnDisplayText; void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomC...

通常我們在寫主題程序或者新聞的程序中..字符太多了需要處理。使用substring()函數處理一下的  所以我想到寫一公共函數:    #region //主題格式    ///     /// 功能: 設置顯示格式: 主題+...    /// 創建時間:2008-12-18    /// 創建人:龔德輝    ///     /// 傳入的參數    /// 顯示的長度  
目標窗體的表格控件不要使用CustomColumnDisplayText,CustomDrawCell等自定義事件 轉載於:https://www.cnblogs.com/tian2008/p/8191387.html
dev GridView常用屬性,事件_weixin_30505485的博客-CSDN博客
10-28
dev GridView常用屬性,事件 一、屬性 1、GridControl屬性 //允許拖拽行gridControl1.AllowDrop =true; 2、GridView屬性 //不可編輯gridView1.OptionsBehavior....
Dev中GridControl中點擊事件_漓塗-CSDN博客
10-24
gridView 行點擊事件: private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e) { System.Data.DataRowView pDataRow...
protected void ASPxGridView1_CustomColumnDisplayText(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDisplayTextEventArgs e) { if (e.Column.VisibleIndex == 8) ...
原始數據格式:下面要求把“數據”設置爲保留2位小數,日期顯示至秒。    運行結果:
DevExpress GridView使用以及按鈕列事件問題_qq_176140..._CSDN博客
11-11
左側欄下邊有個Repository,如圖選擇,最後找到ButtonClike,點擊即可獲取事件。 6.事件中獲取行對象核心代碼: GridView view = ((GridView)(this.gridControl2.MainVie...
DevExpress GridControl中gridview單元格點擊事件(獲取..._CSDN博客
11-8
this.repositoryItemCheckEdit1.NullStyle = DevExpress.XtraEditors.Controls.StyleIndeterminate.InactiveChecked; this.gridView1.SetRowCellValue(selectRow, "IsStart"...
一般設置爲:gridView1.Appearance.HeaderPanel.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; //列頭居中 gridView1.Appearance.Row.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center...
DevExpress.XtraGrid.Views 設置指定行的背景顏色 1.事件:CustomDrawCell2.示例:private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e) { if ...
dev 實現三層GridView嵌套並有點擊事件_育苗小工的博客-CSDN博客
9-12
privatevoidgv1_RowClick(objectsender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e) { //GridView using DevExpress.XtraGrid.Views.Grid ...
關於DevExpress GridControl中gridView1_InitNewRow事..._CSDN博客
11-6
private void gridView1_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e) { MessageBox.Show("觸發事件測試"); ...
數據準備:自義定一個DataTable作爲GridControl的數據源。private DataTable CreatDataTable() { DataTable dt = new DataTable();//創建表 DataColumn dc = new DataColumn(); dc.Caption = "編號"; dc.ColumnName = "I...
支持本地書籤、tab頁、歷史記錄搜索;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章