日期:2014-05-17  浏览次数:20854 次

C# datagridview.cellpaintting事件刷新问题
如何手动调用cellpaintting进行刷新呢?下面是一例,对datagridview的单元格进行重绘,因没有办法手动调用cellpaintting事件,所以,无法对单元格强制绘制,我试用invalidate()进行强行重绘,结果也不调用cellpaintting,而是调用onpaint.

[img=http://my.csdn.net/my/album/detail/1651934][/img]


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DatagridviewTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
           
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.dataGridView1.Rows.Add(10);
        }

        private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                e.Graphics.DrawString(e.RowIndex .ToString (), e.CellStyle.Font, new SolidBrush(Color.Red), e.CellBounds);
                e.PaintContent(e.CellBounds);
                e.Handled = true;
            }
            
            
        }

       
    }
}