DevExpress.XtraGrid winform试用分享

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控件面板中点击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
//添加行号
        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) 已经入职了。

 

 

posted @ 2014-03-31 18:28  数据酷软件  阅读(2980)  评论(4编辑  收藏  举报