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

2020-12-13 14:25

阅读:550

标签:datagridview   style   blog   io   color   os   ar   for   div   

        #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 = 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 = 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 

  

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

标签:datagridview   style   blog   io   color   os   ar   for   div   

原文地址:http://www.cnblogs.com/shenchao/p/4063282.html

上一篇:PHP 数组函数大全

下一篇:c#接口


评论


亲,登录后才可以留言!