2014-07-25 47 views
0

我有一个窗体与UserControl包含车辆列表的网格,我想传递车辆ID和颜色列表的方法,并在该方法中,我想查找每个车辆ID在数据源中的数据源索引。如何更改Devexpress网格行颜色无事件

这些索引我想要获得RowHandle(或直接行对象),并改变我在参数中传递的颜色的背景颜色。

private void ApplyColorRow(List<int> vehicleID, Color color) 
{ 
    var Index = 0; 
    // foreach view the Datasource 
    foreach (var View in this.VehicleViewList) 
    { 
    // if the list of VehicleID contains the vehicleID 
    if (vehicleID.Contains(View.VehicleData.VehicleID)) 
    { 
     // find the Row handle corresping to the datasource index 
     var RowHandle = this.gvVehicle.GetRowHandle(Index); 
     // Get the row object 
     // This return an object corresponding to the View (VehicleView in my case) 
     // But I need the Row object to change the appearance. 
     var Row = this.gvVehicle.GetRow(RowHandle); 
     // Row.BackColor = color; 
    } 
    Index++; 
    } 
} 

回答

2

除非你处理GridView控件RowStyle事件,试图做任何更改将立即撤消一旦电网自我刷新。

是否有原因不希望使用事件来设置行颜色?您可以简单地缓存每个颜色的颜色,然后在RowStyle事件中设置适当的颜色。

这里的DevExpress的文件,概述了自定义行出场: https://documentation.devexpress.com/#WindowsForms/CustomDocument758

+0

它的唯一原因,因为我不想在“加载时间”更改RowStyle,但我找到了解决方案与GridView.RefreshRow(int rowHandle)和处理RowStyle事件。谢谢 –

0

如果条件有限的数量,实际上你可以做到这一点没有任何事件(甚至RowCellStyle),采用网格设计器中的格式条件。如果进入设计模式并选择“外观”,然后选择“格式条件”,则可以添加一系列格式条件及其相应的效果。

每种格式条件都有一个名为“应用于行”的属性,您可以在其中为单个列定义条件,但格式只能应用于该列,其他列或整个行。

缺点是每种颜色都需要一种格式条件,除非您将设计器代码劫持到您的表单代码中(这不一定是个坏主意)。

如果System.Color实际上是您的其中一个属性的数据类型,并且您有无限的(或大)数量的可能性,那么RowCellStyle事件就是要走的路。

顺便说一句,根据您的最新评论,您可以始终在加载过程中禁用RowCellStyle,并在Shown事件完成后将其重新命名。

相关问题