2014-01-24 3 views
2

Я могу установить для всех, используя задний цвет, но как я могу установить цвет текста, как показано ниже в Grid View: Success = green, Process = Red, Verified = Yellow Спасибо всем ,Как установить цветной текст 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 ячейки в поле Template] (http://stackoverflow.com/q/15907217/456814). –

ответ

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 на объект и наши правила получения значения состояния.

GridViewRow.DataItem Property

protected void RowDataBound(Object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     var obj = (MyObject)e.Row.DataItem; 
     if(obj.Status == "Success") 
     (e.Row.FindControl("Label1") as Label).BackColor = Color.Green; 

if(obj.Status== "Process") 
     (e.Row.FindControl("Label1") as Label).BackColor = Color.Rad; 

if(obj.Status == "Verified") 
     (e.Row.FindControl("Label1") as Label).BackColor = Color.Yellow; 
    } 
} 
+0

Привет, Vignesh, я применил первый код, но я получил ошибку: Оператор '==' не может быть применен к операндам типа «Syste.Web.Ul.WebControls.label» и «string» любой помощи, пожалуйста? Зей благодарю вас за сообщение. – 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; 

} 
} 
Смежные вопросы