利用DataGridView 的 RowPostPaint 事件 和 CellFormatting 事件 对行划线

要求:

1,选中行时,不改变行的背景色 ,在行的顶部和底部划线表示

2,当满足条件时,在行的中间划线

下面的例子简单实现这两个要求

/********************************************** 
 * 作    者: DreamDays
 * 
 * 说    明: DataGridViewDemo:行划线
 * 
 * 时    间:2012年05月01日
 *********************************************/
using System.Drawing;
using System.Windows.Forms;

namespace WinFormDataGridViewDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InitData();
        }
        int pass = 450;
        //用来在行中间划线的画笔
        Pen penMid = new Pen(Color.Red);
        //选中行时划线的画笔
        Pen penSelected = new Pen(Color.White);
        private void dgvResult_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rec = e.RowBounds;
            int score = GetNumberByObject(dgvResult.Rows[e.RowIndex].Cells["colResult"].Value);
            if (!IsPass(score, pass))
            {//不及格时 ,在行中间划线
                Point pStart = new Point(rec.X, rec.Y + rec.Height / 2);
                Point pEnd = new Point(rec.X + rec.Width, rec.Y + rec.Height / 2);
                e.Graphics.DrawLine(penMid, pStart, pEnd);
            }
            if (this.dgvResult.CurrentRow.Index == e.RowIndex)
            {//选中行时划线
                Point pTopStart = new Point(rec.X, rec.Y + 2);
                Point pTopEnd = new Point(rec.X + rec.Width, rec.Y + 2);
                Point pBottomStart = new Point(rec.X, rec.Y + rec.Height - 2);
                Point pBottomEnd = new Point(rec.X + rec.Width, rec.Y + rec.Height - 2);
                e.Graphics.DrawLine(penSelected, pTopStart, pTopEnd);
                e.Graphics.DrawLine(penSelected, pBottomStart, pBottomEnd);
            }
        }

        private void dgvResult_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (this.dgvResult.CurrentRow.Index == e.RowIndex)
            {//选中行时不改变行底色
                e.CellStyle.SelectionBackColor = e.CellStyle.BackColor;
                e.FormattingApplied = true;
            }
        }
        /// <summary>
        /// 添加几行数据
        /// </summary>
        private void InitData()
        {
            this.dgvResult.Rows.Add(new string[] { "张三", "500" });
            this.dgvResult.Rows.Add(new string[] { "李四", "451" });
            this.dgvResult.Rows.Add(new string[] { "王五", "500" });
            this.dgvResult.Rows.Add(new string[] { "马六", "449" });
            this.dgvResult.Rows.Add(new string[] { "小明", "410" });
            this.dgvResult.Rows.Add(new string[] { "小花", "550" });
            this.dgvResult.Rows.Add(new string[] { "小强", "510" });
            this.dgvResult.Rows.Add(new string[] { "小小", "500" });
            this.dgvResult.Rows.Add(new string[] { "大牛", "440" });
            this.dgvResult.Rows.Add(new string[] { "二虎", "450" });
        }
        /// <summary>
        /// 判断是否达到及格线
        /// </summary>
        /// <param name="score"></param>
        /// <param name="pass"></param>
        /// <returns></returns>
        private bool IsPass(int score, int pass)
        {
            return score >= pass;
        }
        /// <summary>
        /// 将object转换为int
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        private int GetNumberByObject(object o)
        {
            if (o == null)
            {
                return 0;
            }
            int score = 0;
            int.TryParse(o.ToString(), out score);
            return score;
        }
    }
}

效果图如下:

不及格的行中间划线 ,选中的行用两条白线表示

转载于:https://www.cnblogs.com/DreamDays/archive/2012/05/01/2478080.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值