DataGridView合并单元格(一列或一行)

本文详细介绍了如何在.NET环境中使用DataGridView控件进行单元格的合并操作,包括合并一列或一行的实现方法,旨在提升数据展示的清晰度和用户体验。
摘要由CSDN通过智能技术生成

#region"合并单元格的测试(一列或一行)"

// int?是搜索一种类型(可空类型),普通的int不能为null,而用int?,其值可以为null

//private int? nextrow = null;

//private int? nextcol = null;

 

//在CellPainting方法后调用

private void dataGridView1_CellFormatting(object sender, System.Windows.Forms.DataGridViewCellFormattingEventArgs e)

{

    /*

    //列标示

    if (e.ColumnIndex >= 0 && this.dataGridView1.Columns[e.ColumnIndex].HeaderText == "PARENTID"  && e.RowIndex >= 0)

    {

        //若与下一行同列单元格值相同则修改设置

        if (e.RowIndex < this.dataGridView1.RowCount - 1 && this.dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value != null)

        {

            if (e.Value.ToString() == this.dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString())

            {

                e.CellStyle.BackColor = Color.LightPink;

                this.dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Style.BackColor = Color.LightPink;

                this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ReadOnly = true;

                //this.dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].ReadOnly = true;

            }

        }

         

    }

    */

 

    /*

    //行标示

    if (e.ColumnIndex >= 0 && this.dataGridView1.Columns[e.ColumnIndex].HeaderText == "PHONE1" && e.RowIndex >= 0)

    {

        //若与同行下一列单元格值相同则修改设置

        if (e.ColumnIndex != this.dataGridView1.ColumnCount - 1 && this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value != null)

        {

            if (e.Value.ToString() == this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value.ToString())

            {

                e.CellStyle.BackColor = Color.LightBlue;

                this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Style.BackColor = Color.LightPink;

                this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ReadOnly = true;

                //this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].ReadOnly = true;

            }

        }

    }

     */

}

 

//==========================

 

//绘制单元格

private void dataGridView1_CellPainting(object sender, System.Windows.Forms.DataGridViewCellPaintingEventArgs e)

{

    //纵向合并

    if (e.ColumnIndex >= 0 && this.dataGridView1.Columns[e.ColumnIndex].HeaderText == "PARENTID"  && e.RowIndex >= 0)

    {

        using (

            Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),

            backColorBrush = new SolidBrush(e.CellStyle.BackColor))

        {

            using (Pen gridLinePen = new Pen(gridBrush))

            {

                // 擦除原单元格背景

                e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

 

                /****** 绘制单元格相互间隔的区分线条,datagridview自己会处理左侧和上边缘的线条,因此只需绘制下边框和和右边框

                 DataGridView控件绘制单元格时,不绘制左边框和上边框,共用左单元格的右边框,上一单元格的下边框*****/

                 

                //不是最后一行且单元格的值不为null

                if (e.RowIndex < this.dataGridView1.RowCount - 1 && this.dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value != null)

                {

                    //若与下一单元格值不同

                    if (e.Value.ToString() != this.dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString())

                    {

                        //下边缘的线

                        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,

                        e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);

                        //绘制值

                        if (e.Value != null)

                        {

                            e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font,

                                Brushes.Crimson, e.CellBounds.X + 2,

                                e.CellBounds.Y + 2, StringFormat.GenericDefault);

                        }

                    }

                    //若与下一单元格值相同 

                    else

                    {

                        //背景颜色

                        //e.CellStyle.BackColor = Color.LightPink;   //仅在CellFormatting方法中可用

                        this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.LightBlue;

                        this.dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Style.BackColor = Color.LightBlue;

                        //只读(以免双击单元格时显示值)

                        this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ReadOnly = true;

                        this.dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].ReadOnly = true;

                    }

                }

                //最后一行或单元格的值为null

                else

                {

                    //下边缘的线

                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,

                        e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);

                      

                    //绘制值

                    if (e.Value != null)

                    {

                        e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font,

                            Brushes.Crimson, e.CellBounds.X + 2,

                            e.CellBounds.Y + 2, StringFormat.GenericDefault);

                    }

                }

 

                左侧的线()

                //e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,

                //    e.CellBounds.Top, e.CellBounds.Left,

                //    e.CellBounds.Bottom - 1);

 

                //右侧的线

                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,

                    e.CellBounds.Top, e.CellBounds.Right - 1,

                    e.CellBounds.Bottom - 1);

 

                //设置处理事件完成(关键点),只有设置为ture,才能显示出想要的结果。

                e.Handled = true;

            }

        }

    }

 

    //横向合并

    if (e.ColumnIndex >= 0 && this.dataGridView1.Columns[e.ColumnIndex].HeaderText == "PHONE1" && e.RowIndex >= 0)

    {

        using (

            Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),

            backColorBrush = new SolidBrush(e.CellStyle.BackColor))

        {

            using (Pen gridLinePen = new Pen(gridBrush))

            {

                // 擦除原单元格背景

                e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

 

                /****** 绘制单元格相互间隔的区分线条,datagridview自己会处理左侧和上边缘的线条,因此只需绘制下边框和和右边框

                 DataGridView控件绘制单元格时,不绘制左边框和上边框,共用左单元格的右边框,上一单元格的下边框*****/

 

                //不是最后一列且单元格的值不为null

                if (e.ColumnIndex < this.dataGridView1.ColumnCount - 1 && this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value != null)

                {

                    if (e.Value.ToString() != this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value.ToString())

                    {

                        //右侧的线

                        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top,

                            e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);

                        //绘制值

                        if (e.Value != null)

                        {

                            e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font,

                                Brushes.Crimson, e.CellBounds.X + 2,

                                e.CellBounds.Y + 2, StringFormat.GenericDefault);

                        }

                    }

                    //若与下一单元格值相同 

                    else

                    {

                        //背景颜色

                        //e.CellStyle.BackColor = Color.LightPink;   //仅在CellFormatting方法中可用

                        this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.LightPink;

                        this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Style.BackColor = Color.LightPink;

                        //只读(以免双击单元格时显示值)

                        this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ReadOnly = true;

                        this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].ReadOnly = true;

                    }

                }

                else

                {

                    //右侧的线

                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top,

                        e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);

                      

                    //绘制值

                    if (e.Value != null)

                    {

                        e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font,

                            Brushes.Crimson, e.CellBounds.X + 2,

                            e.CellBounds.Y + 2, StringFormat.GenericDefault);

                    }

                }

                //下边缘的线

                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,

                                            e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);

                e.Handled = true;

            }

        }

 

    }

 

}

 

#endregion

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值