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 |
---|---|
Gets the painted element's appearance settings. |
|
Returns a value specifying limits for the drawing area. |
|
Gets an object which specifies the storage for the most often used pens, fonts and brushes. |
|
Provides information on the painted cell. |
|
Gets the painted cell's value. |
|
Gets the column whose element is being painted. |
|
Gets or sets the painted element's display text. |
|
Gets an object used to paint. |
|
Gets or sets a value specifying whether an event was handled and that the default actions are therefore not required. |
|
Gets the handle of a painted element's row. |
事件包含的参数主要是上面列表展示的数据,在实际的开发中我们需要用到的主要参数也就是这几个,使用的流程是这样的,
1.判断列名称用到Column属性
2.判断单元格的值用到CellValue属性
3.根据单元格的值,设置我们想要的效果,若设这DisplayText,设置Appearence
4.e.Handled设置为true(大部分情况)
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:
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;
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;
}
}
}