Click here to Skip to main content
15,919,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,

I am new to programming.. I have one report I need to highlight the column text with red if that value is "Fail" for columns F1results, F2results , F3results ,F4results, F5results. I did added 5 individual if conditions (you can see in below to display this custom color which is working but I know that it's not good programming so that's why I am posting here , is there anyway I can simply it. Thank you so much.

foreach (var data in alldata)
{
var dto = new ReportDto();
dto.Row.Add("Name", data.Name);
dto.Row.Add("Class", data.Class);
dto.Row.Add("F1results", data.F1results);
dto.Row.Add("F2results", data.F2results);
dto.Row.Add("F3results", data.F3results);
dto.Row.Add("F4results", data.F4results);
dto.Row.Add("F5results", data.F5results);

if (result.F1results == "Fail")
{
dto.CustomColors.Add("F1results", "#da3434");
}
if (result.F2results == "Fail")
{
dto.CustomColors.Add("F2results", "#da3434");
}

if (result.F3results == "Fail")
{
dto.CustomColors.Add("F3results", "#da3434");
}
if (result.F4results == "Fail")
{
dto.CustomColors.Add("F4results", "#da3434");
}
if (result.F5results == "Fail")
{
dto.CustomColors.Add("F5results", "#da3434");
}


finalreportdtos.Add(dto);
}

What I have tried:

I have tried like below , But not sure hw can I update colors to text for respective columns here.

foreach (var column in dto.Row.Keys)
               {
                   if(column == "F1results" || column== "F2results" || column== "F3results" || column == "F4results" || column == "F5results")

                  {

                   }
                }
Posted
Updated 5-Mar-20 7:10am

Perhaps something like this could work for you...
C#
foreach (var column in dto.Row.Keys) {
	if (column.Substring(2, 7) == "results") {
		dto.CustomColors.Add(column, "#da3434");
	}
}
 
Share this answer
 
Comments
chinnuTest 5-Mar-20 15:30pm    
Thank you so much MadMyche It worked. Appreciated for your help..
It's hard to answer your question without seeing the data-types involved. But given the code you've posted, I'm assuming the Row and Column properties are both Dictionary<string, string>, in which case, something like this should work:
C#
foreach (KeyValuePair<string, string> pair in dto.Row)
{
    if (pair.Value == "Fail")
    {
        dto.CustomColors.Add(pair.Key, "#da3434");
    }
}
 
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