如何根据值在DetailsView上设置颜色?

时间:2011-06-20 13:51:23

标签: c# asp.net detailsview

将ASP.NET 4与C#一起使用。

我有一个DetailsView,我在C#中使用代码隐藏填充,基本上将DataTable从SQL Server绑定到DetailsView。我想在细节视图中更改值的颜色,以便正值为绿色,零为默认值,负值为红色。最好的方法是什么?

更新

@Jay我试过这段代码:

private System.Drawing.Color GetColorValue(decimal value)
{
    if (value > 0)
        return System.Drawing.Color.Green;
    else if (value < 0)
        return System.Drawing.Color.Red;
    return System.Drawing.Color.White;
}

protected void dtlOpenTrade_DataBound(object sender, EventArgs e)
{
    foreach (BoundField field in dtlOpenTrade.Fields)
    {
        field.ItemStyle.BackColor = GetColorValue((decimal)dtlOpenTrade.DataItem.GetType().GetProperty(field.DataField).GetValue(dtlOpenTrade.DataItem, null));
    }
}

但我收到此错误:System.NullReferenceException: Object reference not set to an instance of an object.有任何想法吗?

UPDATE2

我用这段代码解决了它,有意义吗?

void RenderDetailsViewColour(DetailsView dtl)
{
    foreach (DetailsViewRow row in dtl.Rows)
    {
        if (row.Cells[0].Text != "TradeId")
        {
            row.Cells[1].BackColor = GetColorValue(Convert.ToDecimal(row.Cells[1].Text));
        }
    }           
}

2 个答案:

答案 0 :(得分:1)

这可能不是“最佳方式”,但您可以在详细信息视图中处理DataBound事件,获取对每个控件的引用,然后根据值更改颜色。

这是一个粗略的例子:

您的业务对象:

public class BusinessObject
{
    public decimal ValueOne { get; set; }
    public decimal ValueTwo { get; set; }
    public decimal ValueThree { get; set; }
    public decimal ValueFour { get; set; }
    public decimal ValueFive { get; set; }
}

DetailsView标记:

<asp:DetailsView ID="DetailsView" runat="server" AutoGenerateRows="false">
    <Fields>
        <asp:BoundField DataField="ValueOne" HeaderText="One" />
        <asp:BoundField DataField="ValueTwo" HeaderText="Two" />
        <asp:BoundField DataField="ValueThree" HeaderText="Three" />
        <asp:BoundField DataField="ValueFour" HeaderText="Four" />
        <asp:BoundField DataField="ValueFive" HeaderText="Five" />
    </Fields>
</asp:DetailsView>

连接事件并在代码隐藏中绑定数据:

        this.DetailsView.DataBound += new EventHandler(DetailsView_DataBound);
        this.DetailsView.DataSource = new BusinessObject[] { myBusinessObject };
        this.DetailsView.DataBind();

编写例程以获取值并返回颜色。这可以扩展为返回一些值,例如background,forcolor等......

    System.Drawing.Color GetColorValue(decimal value)
    {
        if (value > 0)
        {
            return System.Drawing.Color.Green;
        }
        else if (value < 0)
        {
            return System.Drawing.Color.Red;
        }
        return System.Drawing.Color.White;
    }

最后是事件处理程序

void DetailsView_DataBound(object sender, EventArgs e)
{
    foreach (BoundField field in this.DetailsView.Fields)
    {
        field.ItemStyle.BackColor = GetColorValue((decimal)
            this.DetailsView.DataItem.GetType()
            .GetProperty(field.DataField)
            .GetValue(this.DetailsView.DataItem, null));
    }
}

答案 1 :(得分:1)

我的想法和周杰伦一样。

标记:

        <asp:TemplateField HeaderText="Amount Paid" >
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("AmountPaid") %>' />
            </ItemTemplate>
        </asp:TemplateField>

代码:

protected void DetailsView1_DataBound(object sender, EventArgs e)
{
    Label objLabel = (Label)DetailsView1.FindControl("Label1");
    if (objLabel != null)
    {
        Decimal decValue = Convert.ToDecimal(objLabel.Text);
        if (decValue > 0)
        {
            objLabel.ForeColor = System.Drawing.Color.Green;
        }
        else if (decValue < 0)
        {
            objLabel.ForeColor = System.Drawing.Color.Red;
        }
    }
}