2014-01-24 5 views
2

다시 색을 사용하여 모두를 설정할 수 있지만 격자보기에 다음과 같이 색상을 설정하여 텍스트를 설정할 수 있습니다. 성공 = 녹색, 프로세스 = 빨간색, 확인 = 노란색 감사합니다. .GridView의 색상 텍스트 설정 방법

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; 

} 
} 
+0

중복? [템플릿 필드에있는 gridview 셀에서 ASP.NET 변경 텍스트 및 색상] (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 뒤에 코드에

<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

안녕하세요 Vignesh, 첫 번째 코드를 적용했지만 오류가있어 : 연산자 '==' 'Syste.Web.Ul.WebControls.label'형식의 피연산자 및 '문자열'어떤 도움을 적용 할 수 없습니다? Zey는 귀하의 게시물에 감사드립니다. – Brss82

1

사용이 객체를 appropiate과 상태 값을 얻을 수 있습니다.

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