04
7月
2020

Devexpress中GridView同时编辑多个值

今天分享一个实例演示如何在DevexpressGridView中同时编辑多个值,解救那些每天都和报表打交道的“表哥表姐”,给他们朴实无华且枯燥的生活增加一点点的Sweet!

首先,简单介绍一下今天的Demo ,主要由一个GridControl控件构成,包括姓名、工号、手机号和部门列,通过选择“所有”、“行”和“列”模式实现多个单元格同时编辑的效果,主界面如图1所示:
interface

图1-主界面(点击图片可放大)

实战演示:

1.新建一个Windows窗体应用,主界面添加一个GridControl控件,在控件设计中添加“姓名”、“工号”、“手机号”和“部门”列,如图2所示:
guide1

图2-操作指导(点击图片可放大)

2.添加一个CreateData类,用来生成表格数据,代码如下:

using System;
using System.Data;

namespace GridViewEditMultipleValue
{
    public class CreateData
    {
        /// <summary>
        /// 生成表格数据的方法
        /// </summary>
        /// <param name="RowCount"></param>
        /// <returns></returns>
        public static DataTable CreateTable(int RowCount)
        {
            DataTable table = new DataTable();
            table.Columns.Add("Name", typeof(string));
            table.Columns.Add("ID", typeof(string));
            table.Columns.Add("Number", typeof(string));
            table.Columns.Add("Department", typeof(string));

            for (int i = 0; i < RowCount; i++)
            {
                table.Rows.Add(new object[] { String.Format("程序员{0}", 1+i), 1000+i, 13688888880+ i, "编程自修室" });
            }
            return table;
        }

    }
}

3.添加一个MultiSelectionEditingMethod类,用来绑定单元格选中事件调用单元格设置方法实现多个单元格同时编辑功能,代码如下:

using System;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.Utils;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Columns;

namespace GridViewEditMultipleValue
{
    public class MultiSelectionEditingMethod
    {
        private GridView view;
        private RadioGroup radioGroup;

        /// <summary>
        /// 绑定GridView与Radio的事件的方法
        /// </summary>
        /// <param name="view"></param>
        /// <param name="radioGroup"></param>
        public MultiSelectionEditingMethod(GridView view, RadioGroup radioGroup)
        {
            this.radioGroup = radioGroup;
            this.view = view;
            this.view.OptionsBehavior.EditorShowMode = EditorShowMode.MouseDownFocused;
            this.view.MouseUp += view_MouseUp;
            this.view.CellValueChanged += view_CellValueChanged;
            this.view.MouseDown += view_MouseDown;
        }

        /// <summary>
        /// 鼠标按下事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void view_MouseDown(object sender, MouseEventArgs e)
        {
            if (GetInSelectedCell(e))
            {
                GridHitInfo hi = view.CalcHitInfo(e.Location);
                if (view.FocusedRowHandle == hi.RowHandle)
                {
                    view.FocusedColumn = hi.Column;
                    DXMouseEventArgs.GetMouseArgs(e).Handled = true;
                }
            }
        }

        /// <summary>
        /// 单元格数值变化事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void view_CellValueChanged(object sender, CellValueChangedEventArgs e)
        {
            OnCellValueChanged(e);
        }

        bool lockEvents;
        /// <summary>
        /// 使用lockEvents激活SetSelectedCellsValues(e.Value)方法
        /// </summary>
        /// <param name="e"></param>
        private void OnCellValueChanged(CellValueChangedEventArgs e)
        {
            if (lockEvents)
            {
                return;
            }
            lockEvents = true;
            SetSelectedCellsValues(e.Value);
            lockEvents = false;
        }

        /// <summary>
        /// 修改选中单元格数值的方法
        /// </summary>
        /// <param name="value"></param>
        private void SetSelectedCellsValues(object value)
        {
            try
            {
                //数据更新开始,重新绘制GridView
                view.BeginUpdate();

                GridCell[] cells = view.GetSelectedCells();
                ChangeMode mode = (ChangeMode)radioGroup.EditValue;

                foreach (GridCell cell in cells)
                {
                    int rowHandle = cell.RowHandle;
                    GridColumn column = cell.Column;

                    switch (mode)
                    {
                        case ChangeMode.All:
                            break;
                        case ChangeMode.Column:
                            column = view.FocusedColumn;
                            break;
                        case ChangeMode.Row:
                            rowHandle = view.FocusedRowHandle;
                            break;
                    }

                    //通过行与列定位单元格并修改其数值
                    view.SetRowCellValue(rowHandle, column, value);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("" + ex + "");
            }
            finally
            {
                //数据更新结束,绘制新GridView完成
                view.EndUpdate();
            }
        }

        /// <summary>
        /// 获取选中单元格方法
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        private bool GetInSelectedCell(MouseEventArgs e)
        {
            //获取选中单元格的行与列值
            GridHitInfo hi = view.CalcHitInfo(e.Location);
            return hi.InRowCell && view.IsCellSelected(hi.RowHandle, hi.Column);
        }

        /// <summary>
        /// 鼠标抬起事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void view_MouseUp(object sender, MouseEventArgs e)
        {
            bool inSelectedCell = GetInSelectedCell(e);
            if (inSelectedCell)
            {
                DXMouseEventArgs.GetMouseArgs(e).Handled = true;
                view.ShowEditorByMouse();
            }
        }
    }

    /// <summary>
    /// 枚举表格修改模式
    /// </summary>
    public enum ChangeMode
    {
        All,
        Row,
        Column
    }
}

4.主界面代码添加Radio单选框定义、设置GridView多个单元格选择模式和调用MultiSelectionEditingMethod方法,代码如下:

using System;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Grid;

namespace GridViewEditMultipleValue
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            //创建一组单选按钮
            RadioGroup radioGroup = new RadioGroup() { Dock = DockStyle.Top };
            radioGroup.Height = 30;
            Controls.Add(radioGroup);
            radioGroup.Properties.Columns = 3;
            radioGroup.Properties.Items.Add(new DevExpress.XtraEditors.Controls.RadioGroupItem(ChangeMode.All, "所有"));
            radioGroup.Properties.Items.Add(new DevExpress.XtraEditors.Controls.RadioGroupItem(ChangeMode.Row, "行"));
            radioGroup.Properties.Items.Add(new DevExpress.XtraEditors.Controls.RadioGroupItem(ChangeMode.Column, "列"));
            radioGroup.EditValue = ChangeMode.All;

            //创建自定义数据表
            gridControl1.DataSource = CreateData.CreateTable(30);
            gridView1.OptionsSelection.MultiSelect = true;//允许多选
            gridView1.OptionsSelection.MultiSelectMode = GridMultiSelectMode.CellSelect;//多个单元格选择模式

            //创建Number复选框
            RepositoryItemComboBox ri = new RepositoryItemComboBox();
            gridView1.Columns["Number"].ColumnEdit = ri;
            for (int i = 0; i < 10; i++)
            {
                ri.Items.Add(String.Format("1368888888{0}", i));
            }

            //实例化多行编辑方法
            new MultiSelectionEditingMethod(gridView1, radioGroup);
        }      
    }
}

5.编译运行,选择“所有”模式,选中的单元格全部同时编辑 ,效果如图3所示:

result1

图3-“所有”模式效果(点击图片可放大)

选择“行”模式,选中的行全部同时编辑,效果如图4所示:

result2

图4-“行”模式效果(点击图片可放大)

选择“列”模式,选中的列全部同时编辑,效果如图5所示:

result3

图5-”列“模式效果(点击图片可放大)

 


原文链接:https://www.daboke.com/devexpress/gridvieweditmethod.html

B站up主-编程自修室:https://space.bilibili.com/580719958

源码下载:GridViewEditMultipleValue

You may also like...

3 Responses

  1. 匿名说道:

    很好的教程!建议老师制作一篇使用devexpress开发winform的流程:从开发(如何改变主题?是否需要汉化?)到发布(需要包含哪些dev的部件?)!

  2. 匿名说道:

    学习了

发表评论

邮箱地址不会被公开。

微信 OR 支付宝 扫描二维码
为本文作者 打个赏
pay_weixin pay_weixin
最喜欢你一言不合就打赏的样子了^_^