GridView.CustomDrawCell Event事件的應用

Event Data

The event handler receives an argument of type RowCellCustomDrawEventArgs containing data related to this event.

The following RowCellCustomDrawEventArgs properties 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

 

 

The CustomDrawCell event is raised before a data cell is painted. The cell that is going to be painted is identified by theRowCellCustomDrawEventArgs.RowHandle and RowCellCustomDrawEventArgs.Column parameters.

There are two scenarios for using the CustomDrawCell event:

  • To paint data cells manually. The CustomDrawEventArgs.Handled parameter must be set to true. This indicates that you have handled the current call to the CustomDrawCell event 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.Handled parameter must be remain set to false. If the Handled parameter is set to false, the default painting mechanism is invoked for cells after your CustomDrawCell event handler is completed.

    This approach is useful when you need to change the styles of cells (the CustomDrawEventArgs.Appearance parameter), their display texts (the RowCellCustomDrawEventArgs.DisplayText parameter), etc.

    Note

    In some cases, a cell's background may not be repainted by the default painting mechanism after your CustomDrawCell event handler is completed, even if the Handled parameter is set to false. For instance, if you draw a line, this line may be visible even if you set theHandled parameter to false. The cell's default text may be painted over your custom line. However, this is not always true. For instance, if you handle the RowCellStyle event (in addition to the CustomDrawCell event) 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 your CustomDrawCell event 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 the CustomDrawCell event 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 the DisplayText parameter to "Discontinued" and leave the Handled parameter set to false. This text will be drawn using the default appearance settings after performing the event handler.

If a value of the "Discontinued" column is set to false, the background of the "UnitsInStock" cell of the same row is drawn using the LightSkyBlue or "LightGreen" color according to the cell value. In ths case, the Handled parameter is set to true in 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;
        }
    }
}

發佈了112 篇原創文章 · 獲贊 19 · 訪問量 29萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章