C#winform中datagridview合并单元格

苍月惊羽 2013-04-29 03:08:39
大神解救!!C#winform中datagridview空值单元格如何向左合并,求代码求算法!,这个听起来有点绕,我解释下,首先是合并单元格功能啦,然后要求自动检索到空值的单元格,并向左向非空值的单元格合并!真心不懂,虚心求救,如能QQ(1170516873)传授,更加感激不尽
...全文
1035 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
_小黑_ 2013-05-02
  • 打赏
  • 举报
回复
引用 10 楼 kaxier5000 的回复:
[quote=引用 9 楼 zanfeng 的回复:] [quote=引用 5 楼 kaxier5000 的回复:] [quote=引用 2 楼 zanfeng 的回复:] 五一之后发布。。
亲!说好的五一后给程序呢………………[/quote] 我做的不是免费的。 [/quote][/quote] 楼主,研究出来没
苍月惊羽 2013-05-02
  • 打赏
  • 举报
回复
引用 9 楼 zanfeng 的回复:
[quote=引用 5 楼 kaxier5000 的回复:] [quote=引用 2 楼 zanfeng 的回复:] 五一之后发布。。
亲!说好的五一后给程序呢………………[/quote] 我做的不是免费的。 [/quote]
足球中国 2013-05-02
  • 打赏
  • 举报
回复
引用 5 楼 kaxier5000 的回复:
[quote=引用 2 楼 zanfeng 的回复:] 五一之后发布。。
亲!说好的五一后给程序呢………………[/quote] 我做的不是免费的。
bdmh 2013-05-02
  • 打赏
  • 举报
回复
u010523413 2013-05-02
  • 打赏
  • 举报
回复
winform里面还真没弄过,AspNet里面比较简单
_小黑_ 2013-05-02
  • 打赏
  • 举报
回复
同样求解。。。。
苍月惊羽 2013-05-02
  • 打赏
  • 举报
回复
引用 2 楼 zanfeng 的回复:
五一之后发布。。
亲!说好的五一后给程序呢………………
苍月惊羽 2013-05-02
  • 打赏
  • 举报
回复
14楼兄台的代码好面熟………
_小黑_ 2013-05-02
  • 打赏
  • 举报
回复

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex != -1 && Convert.ToString (dataGridView1.Rows[e.RowIndex].Cells[1].Value) == "GP")
            {
                e.CellStyle.Font = new Font(dataGridView1.DefaultCellStyle.Font, FontStyle.Bold);
                e.CellStyle.WrapMode = DataGridViewTriState.True;
                MerageRowSpan(dataGridView1, e, 0, 1);
            }
            if ((e.RowIndex == dataGridView1.Rows.Count - 1) && (e.ColumnIndex == 0))
            {
                Rectangle rect = new Rectangle();
                rect.X = e.CellBounds.X;
                rect.Y = e.CellBounds.Y;
                rect.Width = e.CellBounds.Width;
                rect.Height = e.CellBounds.Height;
                e.Paint(rect, DataGridViewPaintParts.ContentBackground);
                e.PaintBackground(rect, false);
                e.Handled = true;
            }
        }
        private static SortedList rowSpan = new SortedList();//取得需要重新绘制的单元格
        private static string rowValue = "";//重新绘制的文本框内容

        #region  单元格绘制
        /// <summary>
        /// 
        /// DataGridView合并单元格(横向)
        /// </summary>
        /// <param name="dgv">绘制的DataGridview </param>
        /// <param name="cellArgs">绘制单元格的参数(DataGridview的CellPainting事件中参数)</param>
        /// <param name="minColIndex">起始单元格在DataGridView中的索引号</param>
        /// <param name="maxColIndex">结束单元格在DataGridView中的索引号</param>
        public void MerageRowSpan(DataGridView dgv, DataGridViewCellPaintingEventArgs cellArgs, int minColIndex, int maxColIndex)
        {
            if (cellArgs.ColumnIndex < minColIndex || cellArgs.ColumnIndex > maxColIndex) return;

            Rectangle rect = new Rectangle();
            using (Brush gridBrush = new SolidBrush(dgv.GridColor),
                backColorBrush = new SolidBrush(cellArgs.CellStyle.BackColor))
            {
                //抹去原来的cell背景
                cellArgs.Graphics.FillRectangle(backColorBrush, cellArgs.CellBounds);
            }
            cellArgs.Handled = true;

            if (rowSpan[cellArgs.ColumnIndex] == null)
            {
                //首先判断当前单元格是不是需要重绘的单元格
                //保留此单元格的信息,并抹去此单元格的背景
                rect.X = cellArgs.CellBounds.X;
                rect.Y = cellArgs.CellBounds.Y;
                rect.Width = cellArgs.CellBounds.Width;
                rect.Height = cellArgs.CellBounds.Height;

                rowValue += cellArgs.Value.ToString();
                rowSpan.Add(cellArgs.ColumnIndex, rect);
                if (cellArgs.ColumnIndex != maxColIndex)
                    return;
                MeragePrint(dgv, cellArgs, minColIndex, maxColIndex);
            }
            else
            {
                IsPostMerage(dgv, cellArgs, minColIndex, maxColIndex);
            }
        }

        /// <summary>
        /// 不是初次单元格绘制
        /// </summary>
        /// <param name="dgv"></param>
        /// <param name="cellArgs"></param>
        /// <param name="minColIndex"></param>
        /// <param name="maxColIndex"></param>
        public void IsPostMerage(DataGridView dgv, DataGridViewCellPaintingEventArgs cellArgs, int minColIndex, int maxColIndex)
        {
            //比较单元是否有变化
            Rectangle rectArgs = (Rectangle)rowSpan[cellArgs.ColumnIndex];
            if (rectArgs.X != cellArgs.CellBounds.X || rectArgs.Y != cellArgs.CellBounds.Y
                || rectArgs.Width != cellArgs.CellBounds.Width || rectArgs.Height != cellArgs.CellBounds.Height)
            {
                rectArgs.X = cellArgs.CellBounds.X;
                rectArgs.Y = cellArgs.CellBounds.Y;
                rectArgs.Width = cellArgs.CellBounds.Width;
                rectArgs.Height = cellArgs.CellBounds.Height;
                rowSpan[cellArgs.ColumnIndex] = rectArgs;
            }
            MeragePrint(dgv, cellArgs, minColIndex, maxColIndex);

        }

        //画制单元格
        private void MeragePrint(DataGridView dgv, DataGridViewCellPaintingEventArgs cellArgs, int minColIndex, int maxColIndex)
        {

            int width = 0;//合并后单元格总宽度
            int height = cellArgs.CellBounds.Height;//合并后单元格总高度

            for (int i = minColIndex; i <= maxColIndex; i++)
            {
                width += ((Rectangle)rowSpan[i]).Width;
            }

            Rectangle rectBegin = (Rectangle)rowSpan[minColIndex];//合并第一个单元格的位置信息
            Rectangle rectEnd = (Rectangle)rowSpan[maxColIndex];//合并最后一个单元格的位置信息

            //合并单元格的位置信息
            Rectangle reBounds = new Rectangle();
            reBounds.X = rectBegin.X;
            reBounds.Y = rectBegin.Y;
            reBounds.Width = width - 1;
            reBounds.Height = height - 1;


            using (Brush gridBrush = new SolidBrush(dgv.GridColor),
                         backColorBrush = new SolidBrush(cellArgs.CellStyle.BackColor))
            {
                using (Pen gridLinePen = new Pen(gridBrush))
                {
                    // 画出上下两条边线,左右边线无
                    Point blPoint = new Point(rectBegin.Left, rectBegin.Bottom - 1);//底线左边位置
                    Point brPoint = new Point(rectEnd.Right - 1, rectEnd.Bottom - 1);//底线右边位置
                    cellArgs.Graphics.DrawLine(gridLinePen, blPoint, brPoint);//下边线

                    Point tlPoint = new Point(rectBegin.Left, rectBegin.Top);//上边线左边位置
                    Point trPoint = new Point(rectEnd.Right - 1, rectEnd.Top);//上边线右边位置
                    cellArgs.Graphics.DrawLine(gridLinePen, tlPoint, trPoint); //上边线

                    Point ltPoint = new Point(rectBegin.Left, rectBegin.Top);//左边线顶部位置
                    Point lbPoint = new Point(rectBegin.Left, rectBegin.Bottom - 1);//左边线底部位置
                    cellArgs.Graphics.DrawLine(gridLinePen, ltPoint, lbPoint); //左边线

                    Point rtPoint = new Point(rectEnd.Right - 1, rectEnd.Top);//右边线顶部位置
                    Point rbPoint = new Point(rectEnd.Right - 1, rectEnd.Bottom - 1);//右边线底部位置
                    cellArgs.Graphics.DrawLine(gridLinePen, rtPoint, rbPoint); //右边线

                    //计算绘制字符串的位置
                    SizeF sf = cellArgs.Graphics.MeasureString(rowValue, cellArgs.CellStyle.Font);
                    float lstr = (width - sf.Width) / 2;
                    float rstr = (height - sf.Height) / 2;

                    //画出文本框
                    if (rowValue != "")
                    {
                        cellArgs.Graphics.DrawString(rowValue, cellArgs.CellStyle.Font,
                                                   new SolidBrush(cellArgs.CellStyle.ForeColor),
                                                     rectBegin.Left + lstr,
                                                     rectBegin.Top + rstr,
                                                     StringFormat.GenericDefault);
                    }
                }
                cellArgs.Handled = true;
            }

        }
        #endregion
Kim_Du 2013-05-02
  • 打赏
  • 举报
回复
http://blog.sina.com.cn/s/blog_5245a6580100m0lm.html 可以看看
flashposition 2013-05-02
  • 打赏
  • 举报
回复
这个好弄,在cellpaint里自己画,我刚画了个竖的,横的差不多,就画距形啊,drawtext之类的
苍月惊羽 2013-04-29
  • 打赏
  • 举报
回复
引用 2 楼 zanfeng 的回复:
五一之后发布。。
给个链接例子行不咯!我自己去推敲!
苍月惊羽 2013-04-29
  • 打赏
  • 举报
回复
引用 2 楼 zanfeng 的回复:
五一之后发布。。
大神!我五一那天晚上要交作业……
足球中国 2013-04-29
  • 打赏
  • 举报
回复

五一之后发布。。
  • 打赏
  • 举报
回复
哪来的合并单元格功能?

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧