DevExpress.XtraGrid winform试用分享

DevExpress.XtraGrid在winform里使用还挺麻烦,为了减少重复代码和代码复用,本人针对DevExpress.XtraGrid封装了一个Form的基类,其中涵盖了DevExpress.XtraGrid的基本用法,本文没有多少营养大家慎重观评啊,否则浪费您看岛国爱情动作片的宝贵时间本博概不负责!哈哈。

关注点: WinForm项目使用封装继承DevExpress.XtraGrid在WinForm的基本运用。

正文

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.数据绑定

(1) 在GridControl控件面板中点击clip_image002

(2) 在出现的窗体中,点击左边的clip_image004进行列名的编辑。点击上方的clip_image006可添加一列,clip_image008插入一列,clip_image010移除一列。点击clip_image006[1]后在右边的属性面板中找到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//添加行号 word">void gridView2_CustomDrawRowIndicator(word">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)代码:

基类窗体代码:

001. using System;
002. using System.Collections.Generic;
003. using System.ComponentModel;
004. using System.Data;
005. using System.Drawing;
006. using System.Linq;
007. using System.Text;
008. using System.Windows.Forms;
009.
010. namespace DailyAuditApp
011. {
012. public class XtraGridListBaseForm : Form
013. {
014. protected DevExpress.XtraGrid.GridControl gridCtrl;
015. protected DevExpress.XtraGrid.Views.Grid.GridView gridView;
016. private string groupPanelText = "操作提示:拖动某个列标题到此处即可按该列分组统计。";
017. /// <summary>
018. /// 分组面板提示文本
019. /// </summary>
020. public string GroupPanelText
021. {
022. get { return groupPanelText; }
023. set
024. {
025. groupPanelText = value;
026. gridView.GroupPanelText = value;
027. gridView.Invalidate();
028. }
029. }
030. protected bool isDisplayRowIndexNo = true;
031. /// <summary>
032. /// 表格是否显示行号
033. /// </summary>
034. public bool IsDisplayRowIndexNo
035. {
036. get { return isDisplayRowIndexNo; }
037. set { isDisplayRowIndexNo = value; }
038. }
039. private bool enableAppearanceEvenRow = true;
040. /// <summary>
041. /// 隔行显示不同的颜色
042. /// </summary>
043. public bool EnableAppearanceEvenRow
044. {
045. get { return enableAppearanceEvenRow; }
046. set
047. {
048. enableAppearanceEvenRow = value;
049. gridView.OptionsView.EnableAppearanceEvenRow = true;
050. gridView.Invalidate();
051. }
052. }
053. /// <summary>
054. /// 窗体宽度匹配 工作主屏幕
055. /// </summary>
056. private bool fullScreenWidth = true;
057. public bool FullScreenWidth
058. {
059. get { return fullScreenWidth; }
060. set { fullScreenWidth = value;}
061. }
062. /// <summary>
063. /// 构造函数,创建GridControl和GridView
064. /// 定制并初始化Form
065. /// </summary>
066. public XtraGridListBaseForm()
067. {
068. this.Icon = Properties.Resources.aidpoint_ico;
069. this.gridCtrl = new DevExpress.XtraGrid.GridControl();
070. this.gridView = new DevExpress.XtraGrid.Views.Grid.GridView();
071. this.gridCtrl.Dock = DockStyle.Fill;
072. //
073. // gridCtrl
074. //
075. this.gridCtrl.Location = new System.Drawing.Point(156, 130);
076. this.gridCtrl.MainView = this.gridView;
077. this.gridCtrl.Name = "gridCtrl";
078. this.gridCtrl.Size = new System.Drawing.Size(400, 200);
079. this.gridCtrl.TabIndex = 0;
080. this.gridCtrl.ViewCollection.AddRange(new DevExpress.XtraGrid.Views.Base.BaseView[] {
081. this.gridView});
082. //
083. // gridView
084. //
085. this.gridView.GridControl = this.gridCtrl;
086. this.gridView.Name = "gridView";
087. gridView.IndicatorWidth = 30;
088. gridView.GroupPanelText = groupPanelText;
089. //展现表格控件
090. this.Controls.Add(gridCtrl);
091. gridCtrl.BringToFront();
092. }
093.
094. protected override void OnLoad(EventArgs e)
095. {
096. base.OnLoad(e);
097. //主屏幕工作区宽度自适应
098. if (fullScreenWidth)
099. {
100. this.Width = Screen.PrimaryScreen.WorkingArea.Width - 2;
101. this.Top = (Screen.PrimaryScreen.WorkingArea.Height - this.Height) / 2;
102. this.Left = 1;
103. }
104. //隔行显示
105. gridView.OptionsView.EnableAppearanceEvenRow = enableAppearanceEvenRow;
106. if (gridView.GroupCount > 0)
107. gridView.ExpandAllGroups();
108. //自动展开
109. gridView.EndGrouping += new EventHandler(gridView_EndGrouping);
110. //表格显示行号
111. if (isDisplayRowIndexNo)
112. {
113. gridView.CustomDrawRowIndicator += new DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventHandler(gridView_CustomDrawRowIndicator);
114. }
115. //默认数据源
116. if (gridCtrl.DataSource == null)
117. {
118. gridView.OptionsBehavior.Editable = false;
119. SetGridViewDataSource();
120. }
121. }
122.
123. /// <summary>
124. /// 拖拽(列表标头)分组后自动展开
125. /// </summary>
126. /// <param name="sender"></param>
127. /// <param name="e"></param>
128. void gridView_EndGrouping(object sender, EventArgs e)
129. {
130. if (gridView.GroupCount > 0)
131. gridView.ExpandAllGroups();
132. }
133.
134. /// <summary>
135. /// 自动添加行号
136. /// </summary>
137. /// <param name="sender"></param>
138. /// <param name="e"></param>
139. void gridView_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
140. {
141. if (e.Info.IsRowIndicator && e.RowHandle >= 0)
142. {
143. e.Info.DisplayText = (e.RowHandle + 1).ToString();
144. }
145. }
146.
147. /// <summary>
148. /// 回车键焦点跳转到下一TabOrder的控件上,ESC关闭窗口
149. /// </summary>
150. /// <param name="msg"></param>
151. /// <param name="keyData"></param>
152. /// <returns></returns>
153. protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
154. {
155. if ((keyData == Keys.Enter) && (!(ActiveControl is Button)))
156. {
157. System.Windows.Forms.SendKeys.Send("{TAB}");
158. return true;
159. }
160. if (keyData == Keys.Escape)
161. {
162. this.Close();
163. }
164. return base.ProcessCmdKey(ref msg, keyData);
165. }
166.
167. /// <summary>
168. /// 设置表格数据源
169. /// </summary>
170. protected virtual void SetGridViewDataSource()
171. {
172. var dt = new DataTable();
173. dt.Columns.Add(new DataColumn("提示信息",typeof(string)));
174. var dr = dt.NewRow();
175. dr.ItemArray = new string[]{"没有记录。"};
176. dt.Rows.Add(dr);
177. gridCtrl.DataSource = dt;
178. }
179. }
180. }

  

业务窗体代码:

01. using System;
02. using System.Collections.Generic;
03. using System.ComponentModel;
04. using System.Data;
05. using System.Drawing;
06. using System.Linq;
07. using System.Text;
08. using System.Windows.Forms;
09. using DataAccess;
10. using DailyAuditApp.DailyAuditProxy;
11.
12. namespace DailyAuditApp
13. {
14. public partial class Form1 : XtraGridListBaseForm
15. {
16. DailyAuditService biz = new DailyAuditService();
17. public Form1()
18. {
19. InitializeComponent();
20. }
21.
22. private void button1_Click(object sender, EventArgs e)
23. {
24. using (aidpoint_cloudEntities db = new aidpoint_cloudEntities())
25. {
26. var ds = db.ExecuteQuery("SELECT * FROM [aidpoint_cloud].[dbo].[样本]");
27. ds.Tables[0].TableName = "付款方式统计表";
28. biz.SyncClientBizData("Aidpoint4006005262", ds);
29. }
30. }
31.
32. protected override void SetGridViewDataSource()
33. {
34. gridCtrl.DataSource = new DataAccess.aidpoint_cloudEntities().付款方式统计表;
35. gridView.Columns["业态名称"].GroupIndex = 0;
36. }
37. }
38. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值