DevExpress.XtraGrid在winform里使用還挺麻煩,為了減少重復代碼和代碼復用,本人針對DevExpress.XtraGrid封裝了一個Form的基類,其中涵蓋了DevExpress.XtraGrid的基本用法,本文沒有多少營養大家慎重觀評啊,否則浪費您看島國愛情動作片的寶貴時間本博概不負責!哈哈。
關注點: WinForm項目使用封裝、繼承;DevExpress.XtraGrid在WinForm的基本運用。
前戲:
本人已經逃離上海,回老家上成都發展了(繼續做開發,到傳統軟件公司做安卓和.net c/s方向的開發)。求關照,求介紹私活(速度賺錢還房貸啊!!!回成都收入減少那是剛剛嘀)。認識多年的朋友邀請我作為的戰略合作伙伴加入他成都的公司,本作離老家(重慶)近可經常回去和妻兒呆在一起,且收入下降的比例還可以接受,遂接受邀請,心不甘情不願的回來了。去上海十一年,好像沒什么收獲啊,臨走前想找個15k的工作說服自己留下來,天可憐見,不能如願啊。
正文:
1)DevExpress.XtraGrid基本技巧
1.DevExpress控件組中的GridControl控件不能使橫向滾動條有效。現象:控件中的好多列都擠在一起,列寬都變的很小,根本無法正常瀏覽控件單元格中的內容。
解決:
gridView1.OptionsView.ColumnAutoWidth屬性是true,即各列的寬度自動調整,你把它設成false,就會出現了。
2.使單元格不可編輯。
gridcontrol -->gridview -->OptionsBehavior -->Editable=false
3.去除"Drag a Column Header Here To Group by that Column"或者改為中文
屬性Gridview->Option View->Show Group Panel=false,就好了 ,“gridView.GroupPanelText”是設置改默認文字的屬性。
4.數據綁定
(2) 在出現的窗體中,點擊左邊的進行列名的編輯。點擊上方的
可添加一列,
插入一列,
移除一列。點擊
后在右邊的屬性面板中找到Caption設置顯示的列標題和FieldName設置該列綁定數據的字段名,Visible設置列是否隱藏。
綁定代碼:
gridControl2.DataSource = od.data_select("select * from tablename").Tables[0];//od是數據庫操作類,data_select返回DataSet類型,綁定DataTable類型
5.選擇某行數據觸發時間
gridView2.RowClick += new DevExpress.XtraGrid.Views.Grid.RowClickEventHandler(gridView2_RowClick);
這樣設置以后必須點擊最左邊的行編號才可以觸發事件,需要設置gridcontrol -->gridview -->OptionsBehavior -->Editable=false即可點擊任意單元格觸發事件。
6.選擇某行后獲取當前表格數據
this.textBox1.Text = gridView2.GetDataRow(e.RowHandle)["列名"].ToString();
7.設置奇、偶行交替顏色
(1) OptionsView.EnableAppearanceEvenRow =
true;
OptionsView.EnableAppearanceOddRow =
true
;
(2) 設置Appearance.EvenRow.BackColor和Appearance.OddRow.BackColor
8.在每行第一列顯示行號
(1) this.gridView2.IndicatorWidth = 30;//設置顯示行號的列寬
(2) 設置動作gridView2.CustomDrawRowIndicator += new DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventHandler(gridView2_CustomDrawRowIndicator);
1
2
3
4
5
6
7
8
|
//添加行號
void
gridView2_CustomDrawRowIndicator(
object
sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
{
if
(e.Info.IsRowIndicator && e.RowHandle >= 0)
{
e.Info.DisplayText = (e.RowHandle + 1).ToString();
}
}
|
9.根據綁定的數據源自動產生列
gridView2.PopulateColumns();
2)代碼:
基類窗體代碼:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace DailyAuditApp { public class XtraGridListBaseForm : Form { protected DevExpress.XtraGrid.GridControl gridCtrl; protected DevExpress.XtraGrid.Views.Grid.GridView gridView; private string groupPanelText = "操作提示:拖動某個列標題到此處即可按該列分組統計。"; /// <summary> /// 分組面板提示文本 /// </summary> public string GroupPanelText { get { return groupPanelText; } set { groupPanelText = value; gridView.GroupPanelText = value; gridView.Invalidate(); } } protected bool isDisplayRowIndexNo = true; /// <summary> /// 表格是否顯示行號 /// </summary> public bool IsDisplayRowIndexNo { get { return isDisplayRowIndexNo; } set { isDisplayRowIndexNo = value; } } private bool enableAppearanceEvenRow = true; /// <summary> /// 隔行顯示不同的顏色 /// </summary> public bool EnableAppearanceEvenRow { get { return enableAppearanceEvenRow; } set { enableAppearanceEvenRow = value; gridView.OptionsView.EnableAppearanceEvenRow = true; gridView.Invalidate(); } } /// <summary> /// 窗體寬度匹配 工作主屏幕 /// </summary> private bool fullScreenWidth = true; public bool FullScreenWidth { get { return fullScreenWidth; } set { fullScreenWidth = value;} } /// <summary> /// 構造函數,創建GridControl和GridView /// 定制並初始化Form /// </summary> public XtraGridListBaseForm() { this.Icon = Properties.Resources.aidpoint_ico; this.gridCtrl = new DevExpress.XtraGrid.GridControl(); this.gridView = new DevExpress.XtraGrid.Views.Grid.GridView(); this.gridCtrl.Dock = DockStyle.Fill; // // gridCtrl // this.gridCtrl.Location = new System.Drawing.Point(156, 130); this.gridCtrl.MainView = this.gridView; this.gridCtrl.Name = "gridCtrl"; this.gridCtrl.Size = new System.Drawing.Size(400, 200); this.gridCtrl.TabIndex = 0; this.gridCtrl.ViewCollection.AddRange(new DevExpress.XtraGrid.Views.Base.BaseView[] { this.gridView}); // // gridView // this.gridView.GridControl = this.gridCtrl; this.gridView.Name = "gridView"; gridView.IndicatorWidth = 30; gridView.GroupPanelText = groupPanelText; //展現表格控件 this.Controls.Add(gridCtrl); gridCtrl.BringToFront(); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); //主屏幕工作區寬度自適應 if (fullScreenWidth) { this.Width = Screen.PrimaryScreen.WorkingArea.Width - 2; this.Top = (Screen.PrimaryScreen.WorkingArea.Height - this.Height) / 2; this.Left = 1; } //隔行顯示 gridView.OptionsView.EnableAppearanceEvenRow = enableAppearanceEvenRow; if (gridView.GroupCount > 0) gridView.ExpandAllGroups(); //自動展開 gridView.EndGrouping += new EventHandler(gridView_EndGrouping); //表格顯示行號 if (isDisplayRowIndexNo) { gridView.CustomDrawRowIndicator += new DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventHandler(gridView_CustomDrawRowIndicator); } //默認數據源 if (gridCtrl.DataSource == null) { gridView.OptionsBehavior.Editable = false; SetGridViewDataSource(); } } /// <summary> /// 拖拽(列表標頭)分組后自動展開 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void gridView_EndGrouping(object sender, EventArgs e) { if (gridView.GroupCount > 0) gridView.ExpandAllGroups(); } /// <summary> /// 自動添加行號 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void gridView_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e) { if (e.Info.IsRowIndicator && e.RowHandle >= 0) { e.Info.DisplayText = (e.RowHandle + 1).ToString(); } } /// <summary> /// 回車鍵焦點跳轉到下一TabOrder的控件上,ESC關閉窗口 /// </summary> /// <param name="msg"></param> /// <param name="keyData"></param> /// <returns></returns> protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if ((keyData == Keys.Enter) && (!(ActiveControl is Button))) { System.Windows.Forms.SendKeys.Send("{TAB}"); return true; } if (keyData == Keys.Escape) { this.Close(); } return base.ProcessCmdKey(ref msg, keyData); } /// <summary> /// 設置表格數據源 /// </summary> protected virtual void SetGridViewDataSource() { var dt = new DataTable(); dt.Columns.Add(new DataColumn("提示信息",typeof(string))); var dr = dt.NewRow(); dr.ItemArray = new string[]{"沒有記錄。"}; dt.Rows.Add(dr); gridCtrl.DataSource = dt; } } }
業務窗體代碼:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using DataAccess; using DailyAuditApp.DailyAuditProxy; namespace DailyAuditApp { public partial class Form1 : XtraGridListBaseForm { DailyAuditService biz = new DailyAuditService(); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { using (aidpoint_cloudEntities db = new aidpoint_cloudEntities()) { var ds = db.ExecuteQuery("SELECT * FROM [aidpoint_cloud].[dbo].[樣本]"); ds.Tables[0].TableName = "付款方式統計表"; biz.SyncClientBizData("Aidpoint4006005262", ds); } } protected override void SetGridViewDataSource() { gridCtrl.DataSource = new DataAccess.aidpoint_cloudEntities().付款方式統計表; gridView.Columns["業態名稱"].GroupIndex = 0; } } }
3)效果截圖:
后戲:
1)風塵仆仆趕回去朋友卻沒有來接我,背一包,提兩包。看來“戰略合作伙伴”並不代表重視啊,再忙也不用開口說的啊...
2) 已經入職了。