ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

DevExpress GridControl实现奇偶行变色及整行选中实现

2021-07-09 16:29:31  阅读:289  来源: 互联网

标签:奇偶 gridView Views DevExpress 整行 view col XtraGrid


DevExpress GridControl实现奇偶行变色及整行选中实现
背景
GridControl
实现的关键代码如下
窗体的调用代码如下
背景
由于最近项目中,客户指定界面组件要使用DevExpress第三方控件,因此就熟悉了DevExpress中常用控件的一些用法,现把一些重要控件的常见用法进行总结分享。

GridControl
在DevExpress中GridControl控件绝对算的上是一个 重量级控件,也算是最常用的控件之一。我的其中一个应用场景是简单的数据只读显示,要求奇偶行变色,选中行,背景色整体变色(默认选中行时,获取焦点的单元格背景色跟同行其他单元格的背景色是不同的)。先看一些最终的实现效果如下图:


实现的关键代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Wongoing.Gui.DataAnalysis.Common.GridControl
{
    /// <summary>
    /// Dev GridControl辅助类
    /// </summary>
    public class GridControlHelper
    {
        /// <summary>
        /// 禁用选择行样式
        /// </summary>
        /// <param name="view"></param>
        public static void DisableSelectStyle(DevExpress.XtraGrid.Views.Base.ColumnView view)
        {
            if (view is DevExpress.XtraGrid.Views.Grid.GridView)
            {
                DevExpress.XtraGrid.Views.Grid.GridView gridView = view as DevExpress.XtraGrid.Views.Grid.GridView;
                gridView.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.None;           //取消焦点框
                gridView.OptionsSelection.EnableAppearanceFocusedCell = false;                              //禁用单元格焦点
                gridView.OptionsSelection.EnableAppearanceFocusedRow = false;                               //禁用整行焦点
            }
        }

        /// <summary>
        /// 启用选择行样式
        /// </summary>
        /// <param name="view"></param>
        public static void EnableSelectStyle(DevExpress.XtraGrid.Views.Base.ColumnView view)
        {
            if (view is DevExpress.XtraGrid.Views.Grid.GridView)
            {
                DevExpress.XtraGrid.Views.Grid.GridView gridView = view as DevExpress.XtraGrid.Views.Grid.GridView;
                gridView.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFullFocus;   //设置焦点框为整行
                gridView.OptionsSelection.EnableAppearanceFocusedCell = false;                              //禁用单元格焦点
                gridView.OptionsSelection.EnableAppearanceFocusedRow = true;                                //启用整行焦点
            }
        }

        /// <summary>
        /// 设置样式
        /// </summary>
        /// <param name="view"></param>
        public static void SetStyles(DevExpress.XtraGrid.Views.Base.ColumnView view)
        {
            if (view is DevExpress.XtraGrid.Views.Grid.GridView)
            {
                DevExpress.XtraGrid.Views.Grid.GridView gridView = view as DevExpress.XtraGrid.Views.Grid.GridView;

                gridView.OptionsView.ShowGroupPanel = false;                                              //隐藏最上面的GroupPanel
                gridView.OptionsView.ShowIndicator = false;                                               //隐藏指示列

                gridView.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.None;           //设置焦点框为整行
                gridView.OptionsSelection.EnableAppearanceFocusedCell = false;                                  //禁用单元格焦点
                gridView.OptionsSelection.EnableAppearanceFocusedRow = true;                                    //启用整行焦点
                gridView.OptionsSelection.EnableAppearanceFocusedRow = true;                                    //启用整行焦点
                gridView.OptionsSelection.EnableAppearanceHideSelection = false;

                gridView.OptionsView.EnableAppearanceEvenRow = true;                                            //启用偶数行背景色
                gridView.OptionsView.EnableAppearanceOddRow = true;                                             //启用奇数行背景色

                //gridView.Appearance.EvenRow.BackColor = System.Drawing.Color.FromArgb(150, 237, 243, 254);      //设置偶数行背景色
                //gridView.Appearance.OddRow.BackColor = System.Drawing.Color.FromArgb(150, 199, 237, 204);       //设置奇数行背景色
                //gridView.Appearance.FocusedRow.BackColor = System.Drawing.Color.Red;
                //gridView.Appearance.SelectedRow.BackColor = System.Drawing.Color.Red;

            }

            //禁用自动生成列
            view.OptionsBehavior.AutoPopulateColumns = false;
            //禁用自动列宽
            if (view is DevExpress.XtraGrid.Views.Grid.GridView)
            {
                (view as DevExpress.XtraGrid.Views.Grid.GridView).OptionsView.ColumnAutoWidth = false;
            }
            //禁用数据过滤面板
            view.OptionsView.ShowFilterPanelMode = DevExpress.XtraGrid.Views.Base.ShowFilterPanelMode.Never;

            #region 添加列

            view.Columns.Clear();

            int index = 0;
            DevExpress.XtraGrid.Columns.GridColumn col = null;

            col = new DevExpress.XtraGrid.Columns.GridColumn();
            col.FieldName = "NodeText";
            col.Caption = "文件名";
            col.Width = 200;
            col.VisibleIndex = index++;
            view.Columns.Add(col);

            col = new DevExpress.XtraGrid.Columns.GridColumn();
            col.FieldName = "CreateDate";
            col.Caption = "创建日期";
            col.Width = 130;
            col.VisibleIndex = index++;
            view.Columns.Add(col);

            col = new DevExpress.XtraGrid.Columns.GridColumn();
            col.FieldName = "TaskFile";
            col.Caption = "任务文件";
            col.Width = 180;
            col.VisibleIndex = index++;
            view.Columns.Add(col);

            col = new DevExpress.XtraGrid.Columns.GridColumn();
            col.FieldName = "FullPath";
            col.Caption = "完整路径";
            col.Width = 180;
            col.VisibleIndex = index++;
            view.Columns.Add(col);

            col = new DevExpress.XtraGrid.Columns.GridColumn();
            col.FieldName = "Barcode";
            col.Caption = "电池条码";
            col.Width = 180;
            col.VisibleIndex = index++;
            view.Columns.Add(col);

            #endregion

            SetAllowEdit(view, false);                                          //禁用编辑
            SetAllowSort(view, DevExpress.Utils.DefaultBoolean.False);          //禁用排序
            SetAllowFilter(view, false);                                        //禁用数据过滤
        }

        /// <summary>
        /// 禁用编辑
        /// </summary>
        /// <param name="view"></param>
        /// <param name="isAllow"></param>
        public static void SetAllowEdit(DevExpress.XtraGrid.Views.Base.ColumnView view, bool isAllow)
        {
            foreach (DevExpress.XtraGrid.Columns.GridColumn col in view.Columns)
            {
                col.OptionsColumn.AllowEdit = isAllow;
            }
        }
        /// <summary>
        /// 禁用排序
        /// </summary>
        /// <param name="view"></param>
        /// <param name="value"></param>
        public static void SetAllowSort(DevExpress.XtraGrid.Views.Base.ColumnView view, DevExpress.Utils.DefaultBoolean value)
        {
            foreach (DevExpress.XtraGrid.Columns.GridColumn col in view.Columns)
            {
                col.OptionsColumn.AllowSort = value;
            }
        }

        /// <summary>
        /// 禁用数据过滤
        /// </summary>
        /// <param name="view"></param>
        /// <param name="isAllow"></param>
        public static void SetAllowFilter(DevExpress.XtraGrid.Views.Base.ColumnView view, bool isAllow)
        {
            foreach (DevExpress.XtraGrid.Columns.GridColumn col in view.Columns)
            {
                col.OptionsFilter.AllowAutoFilter = isAllow;
                col.OptionsFilter.AllowFilter = isAllow;
            }
        }
    }
}
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
窗体的调用代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ICSharpCode.Core;
using Wongoing.Gui.DataAnalysis.Common;

namespace Wongoing.Gui.DataAnalysis.Dialog
{
    public partial class FrmSearch : DevExpress.XtraEditors.XtraForm
    {
        #region 字段定义

        private string _SelectedFileId = String.Empty;

        #endregion

        #region 构造方法

        public FrmSearch()
        {
            InitializeComponent();
        }

        #endregion

        #region 属性定义

        /// <summary>
        /// 返回选中的文件Id
        /// </summary>
        public string SelectedFileId
        {
            get
            {
                return this._SelectedFileId;
            }
        }

        #endregion

        #region 事件处理

        private void FrmSearch_Load(object sender, EventArgs e)
        {
            //设置GridControl样式
            Common.GridControl.GridControlHelper.SetStyles(this.gridControl1.MainView as DevExpress.XtraGrid.Views.Base.ColumnView);
            //订阅行点击事件
            this.gridView1.RowClick += gridView1_RowClick;

        }

        //行点击事件处理
        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left && e.Clicks == 1)
            {
                Common.GridControl.GridControlHelper.EnableSelectStyle(this.gridControl1.MainView as DevExpress.XtraGrid.Views.Base.ColumnView);
            }
            else if (e.Button == System.Windows.Forms.MouseButtons.Left && e.Clicks == 2)
            {
                this._SelectedFileId = GetSelectedFile(sender);
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }

        //查询按钮
        private void btnSearch_Click(object sender, EventArgs e)
        {
            List<DataTreeNode> data = new List<DataTreeNode>();
            data = DataTreeListHelper.ParseDir(Common.Global.AppConfig.TestDataDir, data);
            var result = data.Where(p => p.NodeType == DataTreeNodeTypes.File);
            if (ceKeyWord.Checked)
            {
                if (String.IsNullOrEmpty(this.txtKeyWork.Text))
                {
                    string msg = "请输入关键字!";
                    DevExpress.XtraEditors.XtraMessageBox.Show(msg, Wongoing.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    result = result.Where(p => p.NodeText.ToLower().Contains(this.txtKeyWork.Text.ToLower()));
                }
            }
            if (ceDateTime.Checked)
            {
                if (String.IsNullOrEmpty(this.deBeginDate.Text))
                {
                    string msg = "请选择开始日期!"; ;
                    DevExpress.XtraEditors.XtraMessageBox.Show(msg, Wongoing.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (String.IsNullOrEmpty(this.deEndDate.Text))
                {
                    string msg = "请选择结束日期!";
                    DevExpress.XtraEditors.XtraMessageBox.Show(msg, Wongoing.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                result = result.Where(p => Wongoing.Basic.DataProcessor.ConvertStrToDateTime(p.CreateDate) >= Wongoing.Basic.DataProcessor.ConvertStrToDateTime(this.deBeginDate.Text));
                result = result.Where(p => Wongoing.Basic.DataProcessor.ConvertStrToDateTime(p.CreateDate) <= Wongoing.Basic.DataProcessor.ConvertStrToDateTime(this.deEndDate.Text));
                Console.WriteLine(this.deBeginDate.Text);
                Console.WriteLine(this.deEndDate.Text);
            }
            if (ceTaskFile.Checked)
            {
                if (String.IsNullOrEmpty(this.txtTaskFile.Text))
                {
                    string msg = "请输入任务文件名!";
                    DevExpress.XtraEditors.XtraMessageBox.Show(msg, Wongoing.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                result = result.Where(p => p.NodeText.ToLower().Contains(this.txtTaskFile.Text.ToLower()));
            }
            if (ceBarcode.Checked)
            {
                if (String.IsNullOrEmpty(this.txtBarcode.Text))
                {
                    string msg = "请输入电池条码!";
                    DevExpress.XtraEditors.XtraMessageBox.Show(msg, Wongoing.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                result = result.Where(p => p.NodeText.Contains(this.txtBarcode.Text));
            }
            this.gridControl1.DataSource = result;
            Common.GridControl.GridControlHelper.DisableSelectStyle(this.gridControl1.MainView as DevExpress.XtraGrid.Views.Base.ColumnView);
        }

        //确定按钮
        private void btnOK_Click(object sender, EventArgs e)
        {
            this._SelectedFileId = GetSelectedFile(sender);
            if (String.IsNullOrEmpty(this._SelectedFileId))
            {
                string msg = "还没有选择任何文件!";
                DevExpress.XtraEditors.XtraMessageBox.Show(msg, Wongoing.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        //取消按钮
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        #endregion

        #region 辅助方法

        /// <summary>
        /// 获取选中的文件信息
        /// </summary>
        /// <param name="sender"></param>
        private string GetSelectedFile(object sender)
        {
            int[] selectedRowHandles = this.gridView1.GetSelectedRows();
            if (selectedRowHandles.Length == 1)
            {
                object selectedRow = this.gridView1.GetRow(selectedRowHandles[0]);
                if (selectedRow is Common.DataTreeNode)
                {
                    Common.DataTreeNode currNode = selectedRow as Common.DataTreeNode;
                    return currNode.Id;
                }
            }
            return String.Empty;
        }

        #endregion
    }
}
————————————————
版权声明:本文为CSDN博主「CodingPioneer」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zlbdmm/article/details/101596646

标签:奇偶,gridView,Views,DevExpress,整行,view,col,XtraGrid
来源: https://blog.csdn.net/fangyuan621/article/details/118609339

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有