user3997016
user3997016

Reputation:

gridview label color change

 protected void getmessagelisting()
    {    
          ds = new DataSet();
            SQL = "Select * from [magaj].[message] where UId='" + Session["UserID"].ToString() + "'  order by ID desc";
            SqlDataAdapter da = new SqlDataAdapter(SQL, _connect());
            da.Fill(ds, "message");
            Grid_Joblist.DataSource = ds;
            Grid_Joblist.Columns[0].Visible = true;
            Grid_Joblist.DataBind();
            Grid_Joblist.Columns[0].Visible = false;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (ds.Tables[0].Rows[i]["isread"].ToString() == "0")
                {
                    //  (e.Row.FindControl("lblsubject") as Label).ForeColor = Color.Yellow;
                    foreach (GridViewRow row in Grid_Joblist.Rows)
                    {
                        Label lblsubject = row.FindControl("lblsubject") as Label;
                        lblsubject.ForeColor = Color.Yellow;

                    }
                }
            }
}

now when i run this then i will bind all the data in proper way but when i want to do like if any data having value of isread=0 then i want to change the color of that label.

but when any 1 row is satisfy with this if condition then it will change the all other label color also..

i want to only change the label color if it is satisfy with isread=0 and for other data which is not satisfy with this then it will be the different color

how can i do this?

Upvotes: 0

Views: 2883

Answers (2)

rushank shah
rushank shah

Reputation: 856

you can do one thing :

just print your isread value in label like :

<asp:Label runat="server" ID="lblisread" Text='<%#Eval("isread")%>' Visible="false"/>

then change your function as below:

   foreach (GridViewRow row in Grid_Joblist.Rows)
    {
        Label lblsubject = row.FindControl("lblsubject") as Label;
        Label lblisread = row.FindControl("lblisread ") as Label;

        if (lblisread..Text == "0")
        {
            lblsubject.ForeColor = Color.Red;

        }
        else
        {
            lblsubject.ForeColor = Color.Yellow;
        }

    }

and its done... !! Cheers..!!!

Upvotes: 2

malkam
malkam

Reputation: 2355

Use GridView RowDataBound event to use set label color.

something like below

void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
  {

    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      // Display the company name in italics.
      System.Data.DataRow row = (System.Data.DataRow)e.Row.DataItem;
      if(row["isread"].ToString()=="0")
        {

           Control l = e.Row.FindControl("lblsubject");
  ((Label)l).ForeColor= System.Drawing.Color.Yellow;

    }

  }

Not tested code.

Check these links for more info:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound%28v=vs.110%29.aspx

Row background color in gridview?

Upvotes: 0

Related Questions