2011-06-20 28 views
0

在C#中使用ASP.NET 4。如何在基於值的DetailsView上設置顏色?

我有一個DetailsView我在C#中使用代碼隱藏填充,基本上綁定從SQL Server DataTableDetailsView。我想在細節視圖中更改值的顏色,以使正值爲綠色,零值爲默認值,負值爲紅色。什麼是最好的方法來做到這一點?

UPDATE

@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)); 
     } 
    }   
} 

回答

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(); 

編寫一個例程以獲取一個值並返回顏色。這可以被擴展到返回幾個值,如背景,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)); 
    } 
} 
+0

感謝周杰倫,我已經更新了我Q,你可以有一個快速瀏覽一下? –

+0

異常來自哪裏?我假設並簡化您的數據是小數。您綁定的對象可能不同。 – Jay

+0

我使用小數,錯誤來自'field.ItemStyle.BackColor = GetColorValue((decimal)dtlOpenTrade.DataItem.GetType()。GetProperty(field.DataField).GetValue(dtlOpenTrade.DataItem,null));' –

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; 
     } 
    } 
} 
+0

這種方式也會起作用,您只需確保您想要的樣式的每個字段都在您的方法中進行了說明。但是,如果你儘可能地創建一個模板,爲什麼不直接從標記中評估風格呢? – Jay