Reputation: 1766
I have a form with a UserControl containing a grid of a vehicle list and I want to pass a List of vehicle IDs and a color to method and in that method I want to find each Index of the Datasource where the Vehicle ID is in the DataSource.
With those index I want to get the RowHandle (or directly the row object) and change the backcolor with the color I passed in the parameters.
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++;
}
}
Upvotes: 1
Views: 6360
Reputation: 16377
If you have finite number of conditions, you can actually do this without any events (even RowCellStyle), using the format conditions within the Grid designer. If you enter design mode and select "Appearance," and then "Format Conditions," you can add a series of format conditions and their corresponding effect.
There is a property for each format condition called "Apply To Row," where you define a condition for a single column, but the format can either apply only to that column, to other columns or the entire row.
The disadvantage is for each color you need one format condition, unless you hijack the designer code into your form code (which is not necessarily a bad idea).
If System.Color is actually the datatype of one of your properties, and you have an indefinite (or large) number of possibilities, then the RowCellStyle
event is the way to go.
By the way, per your last comment, you can always disable RowCellStyle
during load and renable it after the Shown
event completes.
Upvotes: 0
Reputation: 874
Unless you handle the GridView RowStyle event, any changes you attempt to make will be immediately undone once the grid refreshes itself.
Is there a reason you don't want to use an event to set the row color? You could simply cache what the color of each vehicleID should be, and then set the appropriate color in the RowStyle event.
Here's the DevExpress documentation that outlines customizing row appearances: https://documentation.devexpress.com/#WindowsForms/CustomDocument758
Upvotes: 2