Brss82
Brss82

Reputation: 27

How to set GridView's color text

I can set for all by using back color but how can i set color to text as follow to be showed in Grid View: Success = green, Process = Red, Verified = Yellow Thank you all.

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

Upvotes: 2

Views: 813

Answers (2)

Vignesh Kumar A
Vignesh Kumar A

Reputation: 28413

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

Or cast DataItem to appropiate object and get the status value.

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

Upvotes: 1

zey
zey

Reputation: 6103

Use this on code behind

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;

 }
}

Upvotes: 1

Related Questions