The MindFusion Forums
Grid and Spreadsheet Components >> Windows Forms >> Conditional Format for rows?
https://mindfusion.eu/Forum/YaBB.pl?num=1406809382

Message started by mustafa on Jul 31st, 2014 at 12:23pm

Title: Conditional Format for rows?
Post by mustafa on Jul 31st, 2014 at 12:23pm
Hello,

MindFusion ConditionalFormats demo shows how to set fill color of cell if its value is in specified condition. Is there any way to fill red full row length if any one cell in the row is in the condition (< 0 for my project)?

Best Regards,
Mustafa Koç

Title: Re: Conditional Format for rows?
Post by Meppy on Jul 31st, 2014 at 2:12pm
Hi,

This is not possible out of the box, but can be achieved manually by using the following method:


Code (]private void ApplyFormatting(Worksheet sheet, int fromRow, int toRow)
{
     var rows = sheet.Cells.Where(cell => cell.Row >= fromRow && cell.Row <= toRow).GroupBy(cell => cell.Row);
     foreach (var row in rows)
     {
           int rowIndex = row.Key;
           bool met = false;
           foreach (var cell in row)
           {
                 if (cell.Value is double)
                 {
                       if ((double)cell.Value < 0)
                       {
                             met = true;
                             break;
                       }
                 }
           }

           if (met)
                 sheet.Rows[rowIndex):

.Style.Background = new MindFusion.Drawing.SolidBrush(Color.Red);
           else
                 sheet.Rows[rowIndex].Style.Background = null;
     }
}

The method inspects the cells in the specified rows and applies the appropriate background to the entire row. You can initially call this method for all rows in the worksheet. Then you can listen to the Worksheet.CellChanged event and call the method only for the affected row:

[code]activeSheet.CellChanged += (s, e) =>
{
     ApplyFormatting(e.Cell.Worksheet, e.Cell.Row, e.Cell.Row);
};[/code]
However, this event is not raised when the value of a cell is changed indirectly, for example, as a result of a formula reevaluation. If you have formulas in the worksheet, you may need to update all rows again.

Regards,
Meppy

The MindFusion Forums » Powered by YaBB 2.6.11!
YaBB Forum Software © 2000-2024. All Rights Reserved.