Reputation: 1943
My GridView has 3 bound columns: A, B, and C. I want to display the highest value of the 3 columns in bold. How do I do the comparison and set the font to bold (preferably in the aspx file)? Thanks.
<Columns>
<asp:BoundField DataField="A" HeaderText="A" SortExpression="A" />
<asp:BoundField DataField="B" HeaderText="B" SortExpression="B" />
<asp:BoundField DataField="C" HeaderText="C" SortExpression="C" />
</Columns>
To clarify: any of the NUMERIC values in columns A, B and C can be the biggest depending on the row. This is the value I want to set to bold.
Example:
3 **4** 1
**6** 2 0
**9** 1 2
Upvotes: 5
Views: 31228
Reputation: 460108
You need codebehind for this sort of thing. Use RowDataBound
for this purpose:
protected void Grid_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int number;
var highestCells = e.Row.Cells.Cast<TableCell>()
.Where(tc => int.TryParse(tc.Text, out number))
.OrderByDescending(c => int.Parse(c.Text));
foreach(var cell in highestCells)
cell.Font.Bold = true;
}
}
Upvotes: 6
Reputation: 15630
You can do it in rowdatabound method of the gridview.
Ideal way is to get the max value of 3 coloumns from DB itself and just check the value in rawdatabound.
This url helps you to provide an small introduction. Similar to this you can add condition and set the corresponding font style of that column to bold
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx
in the condition you can write this line to make bold
e.Row.Cells[2].Font.Bold = true;
Upvotes: 4
Reputation: 13250
Try this way:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int j = 0; j < e.Row.Cells.Count; j++)
{
e.Row.Cells[j].Style.Add("BORDER-BOTTOM", "#aaccee 1px solid");
e.Row.Cells[j].Style.Add("BORDER-RIGHT", "#aaccee 1px solid");
e.Row.Cells[j].Style.Add("padding-left", "5px");
}
}
Upvotes: 10