zoukankan      html  css  js  c++  java
  • 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;
            }
        }
    }
    


  • 相关阅读:
    C/C++——二维数组与指针、指针数组、数组指针(行指针)、二级指针的用法
    C/C++——C语言数组名与指针
    C/C++——C语言跳出多重循环方法
    知识储备——国际象棋术语中英文对照
    C/C++——C语言库函数大全
    C/C++——C语言常用库函数
    C/C++——[05] 函数
    C/C++——[04] 语句
    C/C++——[03] 注释
    C/C++——[02] 运算符和表达式
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3130075.html
Copyright © 2011-2022 走看看