Click here to Skip to main content
15,881,516 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This code changes the colour of a cell in a DevExpress gridview:

if (e.Column.Name == colProduct.Name) // Gets the column
{
	if (View.GetRowCellValue(e.RowHandle, colProduct).ToString() != "") // Double checks the cell isn't blank
	{
		string SupplierProduct = View.GetRowCellValue(e.RowHandle, colProduct).ToString(); // Get the cell

		if (!string.IsNullOrWhiteSpace(SupplierProduct))
		{
			e.Appearance.BackColor = Color.Crimson; // Changes erroring cell to red                                
			e.Appearance.ForeColor = Color.White; // Changes text colour to white                            

			View.OptionsBehavior.ReadOnly = false; // If there's an error the whole GridView is editable
			View.OptionsBehavior.Editable = true;

			SupplierProductNotFound = false;    // Reset to default                            
		}
	}
}


However when I click on the grid the cell reverts to the original colour.

What I have tried:

I've tried removing the options behaviour code in case that was interfering with the colour but it doesn't.

I have tried
e.Handled = true;


How do I make the cell colour permanent?
Posted
Updated 12-Sep-22 10:31am
v2
Comments
[no name] 8-Sep-22 10:59am    
Drawing is dynamic; I doubt that a one-time setting (backed by defaults) will hold between drawing events. Something that varies with content need to be tied to the event system.

https://docs.devexpress.com/WindowsForms/DevExpress.XtraGrid.Views.Grid.GridView.CustomDrawCell
Will Sewell 9-Sep-22 4:03am    
I had been using that documentation and it states "Once you have performed required custom draw actions, set the e.Handled parameter to true to prevent the default renderer from changing this cell. Otherwise, your custom appearance can be lost."

So I'd imagine the default renderer is still changing the cell.

Any suggestions of what to try would be welcome.

1 solution

In a short: you need to change cell style by handling GridView.RowCellStyle Event | WinForms Controls | DevExpress Documentation[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900