合并单元格并为其设置颜色

B/S:

合并GridView 的单元格并为其设置颜色

public void bind()

    {
        DataSet ds = dbsql.Query("select UserID,UserName,UserBoss,UserLimitIP from VM_SYS_User");
        GridView3.DataSource = ds;
        GridView3.DataBind();
    }
    protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        int rowindex = e.Row.RowIndex;
        //设置颜色 
        if (e.Row.Cells[2].Text == "8")
        {
            e.Row.Cells[2].BackColor = Color.Red;
        }
        if (rowindex - 1 < 0) return;
        //合并单元格
        if (e.Row.Cells[3].Text == GridView3.Rows[rowindex - 1].Cells[3].Text)
        {
            if (GridView3.Rows[row].Cells[3].RowSpan == 0)
            {
                GridView3.Rows[row].Cells[3].RowSpan++;
            }
            GridView3.Rows[row].Cells[3].RowSpan++;
            e.Row.Cells[3].Visible = false;
        }
        else
        {
            row = rowindex;
        }
    }    

C/S:

  public void bind()
        {
            DataSet ds = dbsql.Query("select UserID,UserName,UserBoss,UserLimitIP from VM_SYS_User");
            this.dataGridView1.DataSource = ds.Tables[0];
        }


        //设置颜色
        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.Value.ToString() == "8")
            {
                e.CellStyle.BackColor = Color.Red;
            }
        }
        #region"合并单元格的测试"
        //绘制单元格
        private void dataGridView1_CellPainting_1(object sender, System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
        {
            //纵向合并
            if (this.dataGridView1.Columns["UserLimitIP"].Index == e.ColumnIndex && 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);
                        /**/
                        绘制线条,这些线条是单元格相互间隔的区分线条,
                        因为我们只对列name做处理,所以datagridview自己会处理左侧和上边缘的线条
                        if (e.RowIndex != this.dataGridView1.RowCount - 1)
                        {
                            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((String)e.Value, e.CellStyle.Font,
                                        Brushes.Crimson, e.CellBounds.X + 2,
                                        e.CellBounds.Y + 2, StringFormat.GenericDefault);
                                }
                            }
                        }
                        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((String)e.Value, e.CellStyle.Font,
                                    Brushes.Crimson, e.CellBounds.X + 2,
                                    e.CellBounds.Y + 2, StringFormat.GenericDefault);
                            }
                        }
                        //右侧的线
                        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
                            e.CellBounds.Top, e.CellBounds.Right - 1,
                            e.CellBounds.Bottom - 1);


                        e.Handled = true;
                    }
                }
            }
            //横向合并
            if (this.dataGridView1.Columns["UserName"].Index == e.ColumnIndex && 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);
                        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((String)e.Value, 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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值