`
xitongyunwei
  • 浏览: 927171 次
文章分类
社区版块
存档分类
最新评论

GridView.CustomDrawCell Event事件的应用

 
阅读更多
Event Data

The event handler receives an argument of typeRowCellCustomDrawEventArgscontaining data related to this event.

The followingRowCellCustomDrawEventArgsproperties provide information specific to this event.

Property

Description

Appearance

Gets the painted element's appearance settings.

Bounds

Returns a value specifying limits for the drawing area.

Cache

Gets an object which specifies the storage for the most often used pens, fonts and brushes.

Cell

Provides information on the painted cell.

CellValue

Gets the painted cell's value.

Column

Gets the column whose element is being painted.

DisplayText

Gets or sets the painted element's display text.

Graphics

Gets an object used to paint.

Handled

Gets or sets a value specifying whether an event was handled and that the default actions are therefore not required.

RowHandle

Gets the handle of a painted element's row.

事件包含的参数主要是上面列表展示的数据,在实际的开发中我们需要用到的主要参数也就是这几个,使用的流程是这样的,

1.判断列名称用到Column属性

2.判断单元格的值用到CellValue属性

3.根据单元格的值,设置我们想要的效果,若设这DisplayText,设置Appearence

4.e.Handled设置为true(大部分情况)

Remarks

TheCustomDrawCellevent is raised before a data cell is painted. The cell that is going to be painted is identified by theRowCellCustomDrawEventArgs.RowHandleandRowCellCustomDrawEventArgs.Columnparameters.

There are two scenarios for using theCustomDrawCellevent:

  • To paint data cells manually. TheCustomDrawEventArgs.Handledparameter must be set totrue. This indicates that you have handled the current call to theCustomDrawCellevent and no default painting is required. With this approach, you need to paint cells in their entirety.
  • Change a cell's settings and let the default painting mechanism paint the cell based on the provided settings. TheCustomDrawEventArgs.Handledparameter must be remain set tofalse. If theHandledparameter is set tofalse, the default painting mechanism is invoked for cells after yourCustomDrawCellevent handler is completed.

    This approach is useful when you need to change the styles of cells (theCustomDrawEventArgs.Appearanceparameter), their display texts (theRowCellCustomDrawEventArgs.DisplayTextparameter), etc.

    Note

    In some cases, a cell's background may not be repainted by the default painting mechanism after yourCustomDrawCellevent handler is completed, even if theHandledparameter is set tofalse. For instance, if you draw a line, this line may be visible even if you set theHandledparameter tofalse. The cell's default text may be painted over your custom line. However, this is not always true. For instance, if you handle theRowCellStyleevent (in addition to theCustomDrawCellevent) and customize the cell's background color(s), custom drawing will be cleared and the cell's background will be repainted by the default painting mechanism after yourCustomDrawCellevent handler is completed.

    We do not recommend that you rely on this behavior, as it may change in future.

注意事项:

1.事件发生在数据被绘制在单元格之前(此时行已经有了数据,单元格也有了数据只是还没有paint出来),所以如果你自己给单元格的值(自己paint),必须将e.Handled设置为true;

Examples

The following code demonstrates how to use theCustomDrawCellevent to set up the appearance of"UnitsInStock"column cells. If a cell belongs to the focused row, custom draw is not applied and the cell is drawn by default. Otherwise the background of a cell is drawn according to its value.

Also we do not perform custom painting of a cell if the"Discontinued"column cell of the same row is checked. Instead, we just change theDisplayTextparameter to "Discontinued" and leave theHandledparameter set tofalse. This text will be drawn using the default appearance settings after performing the event handler.

If a value of the"Discontinued"column is set tofalse, the background of the"UnitsInStock"cell of the same row is drawn using theLightSkyBlueor"LightGreen"color according to the cell value. In ths case, theHandledparameter is set totruein order to prevent default cell painting.

using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Base;

private void advBandedGridView1_CustomDrawCell(object sender, RowCellCustomDrawEventArgs e) {
    GridView currentView = sender as GridView;
    if(e.RowHandle == currentView.FocusedRowHandle) return;
    Rectangle r = e.Bounds;
    if(e.Column.FieldName == "UnitsInStock") {
        bool check = (bool)currentView.GetRowCellValue(e.RowHandle, 
          currentView.Columns["Discontinued"]);
        if(check) {
            //Change the text to display
            //The e.Handled parameter is false
            //So the cell will be painted using the default appearance settings
            e.DisplayText = "Discontinued";                    
        }
        else {
            // If the cell value is greater then 50 the paint color is LightGreen, 
            // otherwise LightSkyBlue 
            Brush ellipseBrush = Brushes.LightSkyBlue;
            if (Convert.ToInt16(e.CellValue) > 50) ellipseBrush = Brushes.LightGreen;
            //Draw an ellipse within the cell
            e.Graphics.FillEllipse(ellipseBrush, r);
            r.Width -= 12;
            //Draw the cell value
            e.Appearance.DrawString(e.Cache, e.DisplayText, r);
            //Set e.Handled to true to prevent default painting
            e.Handled = true;
        }
    }
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics