关闭

How to merge grid cells

508人阅读 评论(0) 收藏 举报
分类:


The sample illustrates the way to merge grid cells based on a custom-defined rule. To enable cell merging, it's necessary to set the GridView.OptionsView.AllowCellMerge property to true. If you don't want to merge some columns, set the GridColumn.OptionsColumn.AllowMerge property to false. A merging rule is defined using the GridView.CellMerge event.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < 100; i++)
                dataTable1.Rows.Add(new object[] {i %5, "Product " + i, i %7 });
            gridView1.OptionsView.AllowCellMerge = true;
            for (int i = 0; i < gridView1.Columns.Count; i++)
                if (gridView1.Columns[i].FieldName == "CategoryID")
                    gridView1.Columns[i].OptionsColumn.AllowMerge = DevExpress.Utils.DefaultBoolean.True;
                else
                    gridView1.Columns[i].OptionsColumn.AllowMerge =  DevExpress.Utils.DefaultBoolean.False;
        }

        private void gridView1_CellMerge(object sender, DevExpress.XtraGrid.Views.Grid.CellMergeEventArgs e)
        {
            GridView view = sender as GridView;
            try
            {
                if ((e.Column.FieldName == "CategoryID"))
                {
                    int value1 = Convert.ToInt32(view.GetRowCellValue(e.RowHandle1, e.Column));
                    int value2 = Convert.ToInt32(view.GetRowCellValue(e.RowHandle2, e.Column));

                    e.Merge = (value1 == value2);
                    e.Handled = true;
                    return;
                }
            }
            catch (Exception ex)
            {
            }
        }
    }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:192482次
    • 积分:2206
    • 等级:
    • 排名:第17836名
    • 原创:38篇
    • 转载:109篇
    • 译文:1篇
    • 评论:2条
    文章分类
    最新评论