none
DataGridView重绘行头(RowHeader) RRS feed

答案

  • 你好 JL_Nostalgic,

    我不知道你所说的闪烁是什么时候出现的,在我运行上面那个实例的时候没有发现有闪烁效果啊?

    我们一般都是使用此方法来绘制一些信息在行头,例如下面的代码片段就是使用此时间在行头加上一个行号。

    protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
        { //this method overrides the DataGridView's RowPostPaint event 
         //in order to automatically draw numbers on the row header cells
         //and to automatically adjust the width of the column containing
         //the row header cells so that it can accommodate the new row
         //numbers,
    
          //store a string representation of the row number in 'strRowNumber'
          string strRowNumber = (e.RowIndex + 1).ToString();
          
          //prepend leading zeros to the string if necessary to improve
          //appearance. For example, if there are ten rows in the grid,
          //row seven will be numbered as "07" instead of "7". Similarly, if 
          //there are 100 rows in the grid, row seven will be numbered as "007".
          while (strRowNumber.Length < this.RowCount.ToString().Length) strRowNumber = "0" + strRowNumber;
          
          //determine the display size of the row number string using
          //the DataGridView's current font.
          SizeF size = e.Graphics.MeasureString(strRowNumber, this.Font);
          
          //adjust the width of the column that contains the row header cells 
          //if necessary
          if (this.RowHeadersWidth < (int)(size.Width + 20)) this.RowHeadersWidth = (int)(size.Width + 20);
          
          //this brush will be used to draw the row number string on the
          //row header cell using the system's current ControlText color
          Brush b = SystemBrushes.ControlText;
    
          //draw the row number string on the current row header cell using
          //the brush defined above and the DataGridView's default font
          e.Graphics.DrawString(strRowNumber, this.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2));
    
          //call the base object's OnRowPostPaint method
          base.OnRowPostPaint(e);
        } //end OnRowPostPaint method
    

    如果你有任何疑问,可随时给我回复。

     


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年1月24日 11:29
    版主

全部回复

  • 你好 JL_Nostalgic,

    我不知道你所说的闪烁是什么时候出现的,在我运行上面那个实例的时候没有发现有闪烁效果啊?

    我们一般都是使用此方法来绘制一些信息在行头,例如下面的代码片段就是使用此时间在行头加上一个行号。

    protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
        { //this method overrides the DataGridView's RowPostPaint event 
         //in order to automatically draw numbers on the row header cells
         //and to automatically adjust the width of the column containing
         //the row header cells so that it can accommodate the new row
         //numbers,
    
          //store a string representation of the row number in 'strRowNumber'
          string strRowNumber = (e.RowIndex + 1).ToString();
          
          //prepend leading zeros to the string if necessary to improve
          //appearance. For example, if there are ten rows in the grid,
          //row seven will be numbered as "07" instead of "7". Similarly, if 
          //there are 100 rows in the grid, row seven will be numbered as "007".
          while (strRowNumber.Length < this.RowCount.ToString().Length) strRowNumber = "0" + strRowNumber;
          
          //determine the display size of the row number string using
          //the DataGridView's current font.
          SizeF size = e.Graphics.MeasureString(strRowNumber, this.Font);
          
          //adjust the width of the column that contains the row header cells 
          //if necessary
          if (this.RowHeadersWidth < (int)(size.Width + 20)) this.RowHeadersWidth = (int)(size.Width + 20);
          
          //this brush will be used to draw the row number string on the
          //row header cell using the system's current ControlText color
          Brush b = SystemBrushes.ControlText;
    
          //draw the row number string on the current row header cell using
          //the brush defined above and the DataGridView's default font
          e.Graphics.DrawString(strRowNumber, this.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2));
    
          //call the base object's OnRowPostPaint method
          base.OnRowPostPaint(e);
        } //end OnRowPostPaint method
    

    如果你有任何疑问,可随时给我回复。

     


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年1月24日 11:29
    版主