asp.net
  • gridview
  • 2014-01-24 80 views 2 likes 
    2

    我可以用背景色的所有設置,但我怎麼可以設置顏色爲文本如下被顯示在網格視圖:成功=綠色,工藝=紅色,審覈=黃色 謝謝大家。如何設置GridView的彩色文本

    <asp:Label ID="Label1" BackColor="#006699" runat="server" 
        Text='<%#Eval("Status").ToString()=="S"?"Success":Eval("Status").ToString()=="V"?"Verified":Eval("Status").ToString()=="A"?"Approved":"Process" %>'></asp:Label> 
    
    +0

    重複?: [ASP.NET更改文本和彩色中的GridView細胞在模板字段(http://stackoverflow.com/q/15907217/456814)。 – 2014-08-15 22:31:07

    回答

    1
    // Row Data Bound Event fires after gridview calls DataBind() method. 
    // So if you want to data or check certain conditions before displaying it to the user 
    // this may be correct place to do the changes. 
    protected void RowDataBound(Object sender, GridViewRowEventArgs e) 
    { 
        if (e.Row.RowType == DataControlRowType.DataRow) 
        { 
         var status = (Label)e.Row.FindControl("Label1"); 
         if(status == "Success") 
         (e.Row.FindControl("Label1") as Label).BackColor = Color.Green; 
    
    if(status == "Process") 
         (e.Row.FindControl("Label1") as Label).BackColor = Color.Rad; 
    
    if(status == "Verified") 
         (e.Row.FindControl("Label1") as Label).BackColor = Color.Yellow; 
        } 
    } 
    

    或澆鑄的DataItem到appropiate對象並獲取狀態值。

    GridViewRow.DataItem Property

    ​​3210
    +0

    嗨維涅什,我首先申請代碼,但我得到的錯誤是:操作「==」不能應用於類型「Syste.Web.Ul.WebControls.label」和「串」的操作數的任何幫助嗎? Zey感謝你的發佈。 – Brss82

    1

    使用此代碼上落後

    protected void RowDataBound(Object sender, GridViewRowEventArgs e) 
    { 
    if(e.Row.RowType == DataControlRowType.DataRow) 
    { 
        // Retrieve the underlying data item. In this example 
        // the underlying data item is a DataRowView object. 
        DataRowView rowView = (DataRowView)e.Row.DataItem; 
    
        // Retrieve the state value for the current row. 
        String state = rowView["Label1"].ToString(); 
    
        //format color of the as below 
        if(state == "Success") 
          (e.Row.FindControl("lbl1") as Label).BackColor = Color.Green; 
    
        if(state == "Process") 
          (e.Row.FindControl("lbl1") as Label).BackColor = Color.Rad; 
    
        if(state == "Verified") 
          (e.Row.FindControl("lbl1") as Label).BackColor = Color.Yellow; 
    
    } 
    } 
    
    相關問題