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

        #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

  

时间: 2024-08-13 22:10:17

DataGridView合并单元格(一列或一行)之一的相关文章

DataGridView合并单元格(多行多列合并)

一.点击在拖入的显示控件(TreeList)右上方的箭头,在Treelist任务中选择数据源,添加项目数据源,依次选择数据库.数据集,新建连接,浏览选择数据库(*.mdb),依次点击 下一步,选择“表”,完成. 二.具体代码如下: #region"合并单元格(多行多列)" //需要(行.列)合并的所有列标题名 List<String> colsHeaderText_V = new List<String>(); List<String> colsHe

DataGridView合并单元格

昨天一个同事问我DataGridView单元格合并的问题,一开始按照我的设想是算出两个单元格的Rectangle,然后直接使用e.Graphics.FillRectangle(backColorBrush, rectangle)从新填充下背景色,然后在绘制显示的字符,当然这种想法是行不通的. 下面是我写的一个单元格合并的方法,其实这种方法并不好,只是看上去是把单元格合并了,其实实际DataGridView的列结构是没有发生变化,而且这种重绘的方法并不能编辑,所以我还是建议如果遇到合并单元格的问题

POI 实现合并单元格以及列自适应宽度

POI是apache提供的一个读写Excel文档的开源组件,在操作excel时常要合并单元格,合并单元格的方法是: sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 2)); 自适应列宽度: sheet.autoSizeColumn(1); sheet.autoSizeColumn(1, true); 这两种方式都是自适应列宽度,但是注意这个方法在后边的版本才提供,poi的版本不要太老. 注意:第一个方法在合并单元格的的单元格并不好使,必须用

poi导出excel合并单元格(包括列合并、行合并)

1 工程所需jar包如下:commons-codec-1.5.jarcommons-logging-1.1.jarlog4j-1.2.13.jarjunit-3.8.1.jarpoi-3.9-20121203.jar 2 Code: /** * 导出设备信息Excel * @param form 和 HTTP 请求相关的表格对象 * @param resources 信息资源对象 * @param locale 本地化对象 * @param session HTTP 会话对象 * @param

EasyUi 合并单元格占列显示

$("#TableContainer").datagrid({                url: '',                method: "get",                title: '工资明细列表',                loadMsg: '数据加载中,请稍候...',                nowrap: false,                pageSize: 50,                pag

【记录】解析具有合并单元格的Excel

最近公司让做各种数据表格的导入导出,就涉及到电子表格的解析,做了这么多天总结一下心得. 工具:NOPI 语言:C# 目的:因为涉及到导入到数据库,具有合并单元格的多行必然要拆分,而NPOI自动解析的时候拆分单元格除第一个单元格外其余值都是空,对于列头有合并项目的,数据库设计一般才有合并单元格下面的最小列单元作为数据库字段.于是希望达到这样一个效果.于是有了一个思路就是把读入的Excel复制到新建的Excel,然后再去读新的Excel.总体思路就是把合并单元格所包含的所有最小单元格的值都设置成合并

C# Excel行高、列宽、合并单元格、单元格边框线、冻结

private _Workbook _workBook = null;private Worksheet _workSheet = null;private Excel.Application _excelApplicatin = null; _excelApplicatin = new Excel.Application();_excelApplicatin.Visible = true;_excelApplicatin.DisplayAlerts = true; _workBook = _e

datagridview 纵向 横向 合并单元格

datagridview 单元格合并:纵向以及横向合并参考了csdn上不知哪位的代码,具体哪位找不到连接了. 纵向合并: /// <summary> /// 纵向合并,即合并数据项的值 /// </summary> /// <param name="e"></param> private void DrawCellVer(DataGridViewCellPaintingEventArgs e) { if (e.CellStyle.Alig

table合并单元格 colspan(跨列)和rowspan(跨行)

colspan和rowspan这两个属性用于创建特殊的表格. colspan是“column span(跨列)”的缩写.colspan属性用在td标签中,用来指定单元格横向跨越的列数: 在浏览器中将显示如下: 单元格1 单元格2 单元格3 单元格4 该例通过把colspan设为“3”, 令所在单元格横跨了三列.如果我们将colspan设为“2”,则该单元格将只跨越两列,于是有必要在第一行插入另外一个单元格,以确保两行占据相同的列数. 该例在浏览器中将显示如下: 单元格1 单元格2 单元格3 单元