我有一个包含车辆列表网格的UserControl表单,我希望将车辆ID列表和颜色列表传递给方法,在该方法中,我希望找到数据资源的每个索引,其中车辆ID位于DataSource中。
对于这些索引,我希望获得RowHandle (或直接是row对象),并使用参数中传递的颜色更改背景色。
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++;
}
}发布于 2014-07-25 16:11:33
除非您处理GridView RowStyle事件,否则一旦网格刷新自身,您试图进行的任何更改都将立即撤消。
您不想使用事件设置行颜色有什么原因吗?您可以简单地缓存每个vehicleID的颜色,然后在RowStyle事件中设置适当的颜色。
下面是概述自定义行外观的DevExpress文档:https://documentation.devexpress.com/#WindowsForms/CustomDocument758
发布于 2014-07-25 18:05:23
如果您有有限的条件,您实际上可以使用网格设计器中的格式条件,在没有任何事件(甚至是RowCellStyle)的情况下这样做。如果进入设计模式并选择“外观”,然后选择“格式条件”,则可以添加一系列格式条件及其相应的效果。
每个格式条件都有一个名为"Apply To Row“的属性,您可以为单个列定义一个条件,但该格式只能应用于该列、其他列或整个行。
缺点是每种颜色都需要一种格式条件,除非将设计器代码劫持到表单代码中(这不一定是个坏主意)。
如果System.Color实际上是属性之一的数据类型,并且您有不确定的(或大量的)可能性,那么RowCellStyle事件就是最好的选择。
顺便说一句,根据您的最后一个注释,在加载期间始终可以禁用RowCellStyle,并在Shown事件完成后将其重命名。
https://stackoverflow.com/questions/24958169
复制相似问题