2012年5月1日星期二

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

利用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;        }    }}

效果图如下:

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


TAG:WinForm