在datagridview中合并单元格

5
有没有办法在 .NET 的 WinForms 中合并 datagridview 中的单元格。我想要合并一行中的两个或更多单元格。

你想合并行还是列,还是两者都要? - SwDevMan81
@SwDevMan81 我想要合并一行中的两个或更多单元格。 - Thunder
你是怎么让它工作的?被接受的答案链接不起作用,请帮忙分享。 - Darshana
1个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
10

您需要实现OnPaint事件并自己完成。以下是一个示例。 您还可以查看SourceGrid

链接已失效,因此从Web缓存中提取代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Drawing.Drawing2D;


namespace DataGridView_MergeDemo
{
    public class HMergedCell : DataGridViewTextBoxCell
    {
        private int m_nLeftColumn = 0;
        private int m_nRightColumn = 0;

        /// <summary>
        /// Column Index of the left-most cell to be merged.
        /// This cell controls the merged text.
        /// </summary>
        public int LeftColumn
        {
            get
            {
                return m_nLeftColumn;
            }
            set
            {
                m_nLeftColumn = value;
            }
        }

        /// <summary>
        /// Column Index of the right-most cell to be merged
        /// </summary>
        public int RightColumn
        {
            get
            {
                return m_nRightColumn;
            }
            set
            {
                m_nRightColumn = value;
            }
        }

        protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            try
            {
                int mergeindex = ColumnIndex - m_nLeftColumn;
                int i;
                int nWidth;
                int nWidthLeft;
                string strText;

                Pen pen = new Pen(Brushes.Black);

                // Draw the background
                graphics.FillRectangle(new SolidBrush(SystemColors.Control), cellBounds);

                // Draw the separator for rows
                graphics.DrawLine(new Pen(new SolidBrush(SystemColors.ControlDark)), cellBounds.Left, cellBounds.Bottom - 1, cellBounds.Right, cellBounds.Bottom - 1);

                // Draw the right vertical line for the cell
                if (ColumnIndex == m_nRightColumn)
                    graphics.DrawLine(new Pen(new SolidBrush(SystemColors.ControlDark)), cellBounds.Right - 1, cellBounds.Top, cellBounds.Right - 1, cellBounds.Bottom);

                // Draw the text
                RectangleF rectDest = RectangleF.Empty;
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Center;
                sf.LineAlignment = StringAlignment.Center;
                sf.Trimming = StringTrimming.EllipsisCharacter;

                // Determine the total width of the merged cell
                nWidth = 0;
                for (i = m_nLeftColumn; i <= m_nRightColumn; i++)
                    nWidth += this.OwningRow.Cells[i].Size.Width;

                // Determine the width before the current cell.
                nWidthLeft = 0;
                for (i = m_nLeftColumn; i < ColumnIndex; i++)
                    nWidthLeft += this.OwningRow.Cells[i].Size.Width;

                // Retrieve the text to be displayed
                strText = this.OwningRow.Cells[m_nLeftColumn].Value.ToString();

                rectDest = new RectangleF(cellBounds.Left - nWidthLeft, cellBounds.Top, nWidth, cellBounds.Height);
                graphics.DrawString(strText, new Font("Arial", 10, FontStyle.Regular), Brushes.Black, rectDest, sf);
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.ToString());
            }
        }

    }// class
}

https://forums.codeguru.com/showthread.php?415930-DataGridView-Merging-Cells


两个链接都无效,请提供类似的参考资料,如果您有的话。 - Darshana
@Darshana - 已添加失效链接的源代码,并更新了第二个存储库路径。 - SwDevMan81

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,