Datagridview - 在datagridview问题c#windows窗体上绘制矩形

时间:2010-07-02 22:46:26

标签: windows forms datagridview

我有3个问题:

  • 我正在以一种好的方式完成我的任务

  • 为什么当我滚动dataGridView时,画的矩形消失了..

  • 为什么画画这么慢......

下面是我想在每列中绘制一个带有文本的彩色矩形的代码,这些矩形具有相同的值,空值不应该有矩形

void DataGridView1CellPainting(object sender,DataGridViewCellPaintingEventArgs e)         {

foreach(this.dataGridView1.Columns中的DataGridViewColumn列){

            string tempCellValue = string.Empty;
            int tempRectX = -1;
            int tempRectY = -1;
            int tempRectYEnd = -1;
            int tempRectWidth = -1;
            int tempRectHeight = -1;

            foreach (DataGridViewRow row in this.dataGridView1.Rows){

                Rectangle rect = this.dataGridView1.GetCellDisplayRectangle(
                    column.Index, row.Index,true);

                DataGridViewCell cell = dataGridView1.Rows[row.Index].Cells[column.Index];
                if (  cell.Value!=null){


                    if (tempRectX==-1){
                        tempRectX = rect.Location.X;
                        tempRectY = rect.Location.Y;
                        tempCellValue = cell.Value.ToString();
                    }else
                        if (cell.Value.ToString()!=tempCellValue){
                        tempRectYEnd = rect.Location.Y;

                        Rectangle newRect = new Rectangle(tempRectX,
                                                          tempRectY , 5  ,
                                                          tempRectYEnd  );
                        using (
                            Brush gridBrush = new SolidBrush(Color.Coral),
                            backColorBrush = new SolidBrush(Color.Coral))
                        {
                            using (Pen gridLinePen = new Pen(gridBrush))
                            {

                                e.Graphics.FillRectangle(backColorBrush,newRect);

                            }    }
                        tempRectX=-1;
                        tempCellValue = string.Empty;
                    }
                     }else if (tempRectX!=-1){
                    tempRectYEnd = rect.Location.Y;
                    Rectangle newRect = new Rectangle(tempRectX,
                                                      tempRectY , 50  ,
                                                      tempRectYEnd  );
                    using (
                        Brush gridBrush = new SolidBrush(Color.Coral),
                        backColorBrush = new SolidBrush(Color.Coral))
                    {
                        using (Pen gridLinePen = new Pen(gridBrush))
                        {

                            e.Graphics.FillRectangle(backColorBrush,newRect);

                        }    }
                    tempRectX=-1;
                    tempCellValue = string.Empty;
                }
            }}

1 个答案:

答案 0 :(得分:1)

DataGridView1CellPainting事件旨在为一个单元格绘制或更改绘制行为。

DGV为每个可见的Cell提出此事件。

当绘制其他单元格时,代码会变慢。

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcellpaintingeventargs.aspx