We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Setting Cell Background Brush

I'm having trouble setting a given cells background brush and having it behave consistently. 

Right now I'm trying this C#:
spreadsheet.GridProperties.CurrentExcelGridModel[row, column].Background = Brushes.Black;

The issue is it's inconsistent. Sometimes it works initially, but even if it works on a given cell as soon as the cell loses focus or is re-selected the background property reverts to white. What methodology should I be using to permanently change a cell color?

7 Replies

PS Pannir Selvam S Syncfusion Team June 6, 2013 04:55 PM UTC

Hi Austin,

 

Thanks for using Syncfusion product.

 

You can set the background color of the cell in Spreadsheet control by setting the Color of the XlsIO range like in the below code snippet.

 

Code Snippet[C#]:

IRange range = this.spreadSheetControl.ExcelProperties.WorkBook.ActiveSheet[3,3,5,5];

var clr = Colors.Blue;

range.CellStyle.Color = System.Drawing.Color.FromArgb(clr.A, clr.R, clr.G, clr.B);

this.spreadSheetControl.GridProperties.ActiveSpreadsheetGrid.InvalidateCell(GridRangeInfo.Cells(3, 3, 5, 5));

 

 

We have prepared a sample based on this and you can find the sample from the below location.

 

Please let us know if you have any other queries.

 

Thanks,

Pannir



SpreadsheetDemo_WPF_376d7067.zip


AF Austin Fay June 7, 2013 04:18 PM UTC

I attempted to reapply the code provided for my purposes of coloring a single cell but received only UI lag and no color change for my troubles.

I also attempted to load the example provided and while I did observe the code I was unable to build due to missing the Windows. Interactivity reference. I'm using Windows 8 with VS 2012.

Here is what I am executing:
IRange range = spreadsheet.ExcelProperties.WorkBook.ActiveSheet[row, column];
var clr = Colors.Blue;
range.CellStyle.Color = System.Drawing.Color.FromArgb(clr.A, clr.R, clr.G, clr.B);
spreadsheet.GridProperties.ActiveSpreadsheetGrid.InvalidateCell(GridRangeInfo.Cell(row, column));




AF Austin Fay June 7, 2013 07:46 PM UTC

After installing the Blend SDK I got the sample solution to build. Still getting the weirdest behavior when trying to execute even the exact same code you posted. If I tie the code to a button click, the colors won't take effect until I preform a scroll  action on the excel control. I am successfully changing the colors though.


AF Austin Fay June 11, 2013 05:16 PM UTC

Still waiting on a means to refresh the grid or force a scroll action so these colors show up on a button click.


PS Pannir Selvam S Syncfusion Team June 12, 2013 04:45 AM UTC

Hi Austin,

 

Apologize for the delay.

 

You can achieve your requirement by calling the InvalidateVisual() of grid after apply the background color to the cell like in the below code snippet.

 

Code Snippet[C#]:

var grid = this.spreadSheetControl.GridProperties.ActiveSpreadsheetGrid;

IRange range = this.spreadSheetControl.ExcelProperties.WorkBook.ActiveSheet[3, 3];

var clr = Colors.Blue;

range.CellStyle.Color = System.Drawing.Color.FromArgb(clr.A, clr.R, clr.G, clr.B);

grid.InvalidateCell(GridRangeInfo.Cell(3, 3));

grid.InvalidateVisual();

 

 

We have attached a sample based on this and you can find the sample from the below location.

 

Please let us know if you have any other queries.

 

Thanks,

Pannir



SpreadsheetDemo_WPF_4a08a828.zip


AF Austin Fay June 12, 2013 12:37 PM UTC

Thank you,  the InvalidateVisual() method addressed my issues.


PS Pannir Selvam S Syncfusion Team June 14, 2013 11:59 AM UTC

Hi Austin,

 

Thanks for your update.

 

Regards,

Pannir


Loader.
Live Chat Icon For mobile
Up arrow icon