DevExpress.XtraGrid winform试用分享

时间:2014-04-02 12:09:46   收藏:0   阅读:673

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

(1) 在GridControl控件面板中点击bubuko.com,布布扣

(2) 在出现的窗体中,点击左边的bubuko.com,布布扣进行列名的编辑。点击上方的bubuko.com,布布扣可添加一列,bubuko.com,布布扣插入一列,bubuko.com,布布扣移除一列。点击bubuko.com,布布扣后在右边的属性面板中找到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)代码:

基类窗体代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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;
        }
    }
}

  

业务窗体代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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)效果截图: 

bubuko.com,布布扣

bubuko.com,布布扣

bubuko.com,布布扣

 

后戏:

1)风尘仆仆赶回去朋友却没有来接我,背一包,提两包。看来“战略合作伙伴”并不代表重视啊,再忙也不用开口说的啊...

2) 已经入职了。

DevExpress.XtraGrid winform试用分享,布布扣,bubuko.com

原文:http://www.cnblogs.com/datacool/p/dev_XtraGrid_datacool_2014.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!