0

I have a WebGrid in my page (WebMatrix - Razor - C#) that is currently like this:

@assessmentsGrid.GetHtml(
tableStyle: "grid",
headerStyle: "gridHeader",
alternatingRowStyle: "gridAlt",
footerStyle: "gridFooter",
columns: 
assessmentsGrid.Columns(assessmentsGrid.Column(null, null, format: @<text><a href="~/Admin/Assessments/AssessmentDetails/@item.assId">Details</a></text>), 
assessmentsGrid.Column("assId", "ID#"), assessmentsGrid.Column("Date", "Date",format: @<text>@((item.Date == null ||item.Date.Year == 1900) ? "" : item.Date.ToString("d MMM yyyy"))</text>), 
assessmentsGrid.Column("Title", "Title"), 
assessmentsGrid.Column("Forename", "Forename"), 
assessmentsGrid.Column("Surname", "Surname"), 
assessmentsGrid.Column("LocationName", "Location"), 
assessmentsGrid.Column("ActivityType", "Activity Type"), 
assessmentsGrid.Column("Q01", "Q01"), 
assessmentsGrid.Column("Q02", "Q02"), 
assessmentsGrid.Column("Q03", "Q03"), 
assessmentsGrid.Column("Q04", "Q04"), 
assessmentsGrid.Column("Q05", "Q05"), 
assessmentsGrid.Column("Q06", "Q06"), 
assessmentsGrid.Column("Q07", "Q07"), 
assessmentsGrid.Column("Q08", "Q08"), 
assessmentsGrid.Column("Q09", "Q09"), 
assessmentsGrid.Column("Q10", "Q10"), 
assessmentsGrid.Column("Q11", "Q11"), 
assessmentsGrid.Column("Q12", "Q12"), 
assessmentsGrid.Column("Q13", "Q13"), 
assessmentsGrid.Column("Q14", "Q14")
)
)

The content of each Q01-Q14 cell is an integer between 1 and 5.

What I want is to set the background colour of each cell to: red if it's 1, light red if it's 2, white if it's 3, light green if it's 4, green if it's 5.

Could someone give me a steer on the easiest way of doing that? Many thanks.

P.S. I think I know that cell formatting of this nature is one of the limitations of the WebGrid, so as far as I can tell my options are to incorporate some JavaScript/jquery, or go back to the good old fashioned way of creating a table before WebGrid came along.

pnuts
  • 58,317
  • 11
  • 87
  • 139
  • http://stackoverflow.com/questions/20758819/if-else-in-web-grid-column – Can Ürek Mar 08 '16 at 10:04
  • @CanÜrek - Thanks, but as far as I can tell, whatever I put in the then or else area would only format the text, not the table cell containing the text. – Stuart Newbridge Mar 08 '16 at 11:08
  • http://yassershaikh.com/how-to-change-width-style-for-a-column-of-a-webgrid-in-mvc-3-razor/ – Can Ürek Mar 09 '16 at 09:49
  • @CanÜrek - Thanks, but that's a solution for setting the style of all cells in a column. I need to set different styles to each cell, probably through the format: option. – Stuart Newbridge Mar 09 '16 at 11:38
  • You can use something like that. grid.Column("DataBaseColumnName", "Store", format: @@item.DataBaseColumnName) For more advanced solution you can look for extensions. https://gist.github.com/steelheaddigital/3211605 – Can Ürek Mar 10 '16 at 08:29

0 Answers0