I'd like to add a inside a gridview cell. More particularly, the datasource is a number between 1 and 7 and based on that number, the div needs to be of a certain background color and the text need to be a certain word. For instance, if the data is 1, the div should be yellow and the word should be sunny.
What's the best way to do this?
Thank开发者_StackOverflow社区s.
Add this to RowDataBound
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow)
{
int value = (int)DataBinder.Eval(e.Row.DataItem, e.Row.Cells[2].Text);
// e.Row.Cells[2] references the cell value you want to use
if (value < 100)
{
e.Row.Cells[2].BackColor = Color.FromName("#c6efce");
}
if ((value >= 100) && (value < 500))
{
e.Row.Cells[2].BackColor = Color.FromName("#ffeb9c");
}
}
}
You can change it on RowDataBound event of the GridView like as explained in
GridView : Working with TemplateFields
or
You can specify the details in your query like
select
case when [number] = 1 then 'Sunny'
when [number] = 2 then 'Rainy'
end as [DisplayWord],
case when [number] = 1 then 'Yellow'
when [number] = 2 then 'Red'
end as [DisplayColor]
from [YourTable]
and Bind it
I prefer the second method
精彩评论