日期:2014-05-18  浏览次数:20430 次

模版列里单元格的样式
C# code

<asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 160px; POSITION: absolute; TOP: 112px"
                runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<table width="2100" border="1" cellpadding="0" cellspacing="0" bordercolor="#CCCCCC" style="font-family:Arial, Helvetica, sans-serif;font-size:13px;">
<tr>
<td width="52"><%# DataBinder.Eval(Container, "DataItem.Name") %></td>
<td width="52"><%# DataBinder.Eval(Container, "DataItem.Age") %></td>
                                                                
</tr>
</table>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>



当绑定的Age>20,Age所在单元格里显示的字体为红色,怎么实现?



------解决方案--------------------
在后台写一函数
protected string GetColor(object obj)
{
if ( obj != null && obj.ToString().Trim() != "" )
{
if ( int.Parse(obj.ToString().Trim()) > 20 )
return "<font color='red'>" + obj.ToString().Trim() + "</font>";
}
}

然后前台的<td width="52"><%# DataBinder.Eval(Container, "DataItem.Age") %></td>
改成<td width="52">GetColor(<%# DataBinder.Eval(Container, "DataItem.Age") %>)</td>

------解决方案--------------------
C# code
public string GetAge(object o){
   return Convert.ToInt32(o) > 20 ? "<font color='red'>"+o.ToString()+"</font>" : o.ToString();
}

------解决方案--------------------
C# code
/// <summary>
    ///  绑定数据时发生事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridViewDetails_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        switch (e.Row.RowType)
        {
            case DataControlRowType.Header:
                break;
            case DataControlRowType.Footer:
                break;
            case DataControlRowType.DataRow:
                TableCell tc = e.Row.Cells[11];
                if (tc.Text.Trim() != "" && tc.Text.Trim() != "&nbsp;")
                {
                    tc.Attributes.Add("style", int.Parse(tc.Text.Trim()) > 0 ? "color:#FF0000" : "color:#458B00");
                }
                break;
            default:
                break;
        }
    }