Ion
Ion

Reputation: 559

Conditional formatting to entire row

I have a code in C# with EPPLUS for Excel that fills a cell green if the value of the cell is over 100. It works:

ExcelAddress _formatRangeAddress = new ExcelAddress("G4:G" + (c.Count + 4));    
string _statement = "IF(G4>100,1,0)";
var _cond2 = hoja.ConditionalFormatting.AddExpression(_formatRangeAddress);
_cond2.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
_cond2.Style.Fill.BackgroundColor.Color = System.Drawing.Color.LimeGreen;
_cond2.Formula = _statement;

But what I really need is to fill the entire row. If I change the range:

ExcelAddress _formatRangeAddress = new ExcelAddress("A4:G" + (c.Count + 4));  

Applies only for the cells in the A column, not to the entire row.

What I am doing wrong?

Upvotes: 1

Views: 1148

Answers (1)

Ernie S
Ernie S

Reputation: 14250

Make the reference to G4 absolute otherwise the formula will be apply relative to that cell. So change this line:

string _statement = "IF(G4>100,1,0)";

to apply to the entire row based ONLY on G4

string _statement = "IF($G$4>100,1,0)";

(Response to Comments)

to apply to entire rows relative to row number but keep the column fixed to G:

string _statement = "IF($G4>100,1,0)";

Upvotes: 3

Related Questions