Question : How to change color in datarow in gridview in Web Part??

Hii All,
 i am using wss 3.0 and i made some custom webpart to display sharepoint list. I used datarow and column to fill. I am able to display Sharepoint list in gridview but i need some color format accourding to some column like if status == "In Progress" then i need green color for this entire row. So i am stuck with gridview format. i am attaching my code please help me to find better solution for formating Gridview..

So please if you know beter way then let me know..
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
SPList Projectlist = web.Lists["ListA"];
                SPQuery queryProjectList = new SPQuery();
                queryProjectList.ViewFields = "";

                queryProjectList.Query = """;
                SPListItemCollection colProjectlist = Projectlist.GetItems(queryProjectList);
               
                dt = colProjectlist.GetDataTable();
                foreach (DataRow dr in dt.Rows)
                {
                    DataRow newrow = dtProjectList.NewRow();
                    try
                    {
                       
                        newrow["Project"] = dr["Title"];

                        newrow["StartDate"] = DateTime.Parse(dr["Rem_StartDate"].ToString()).ToShortDateString();
                        newrow["EndDate"] = DateTime.Parse(dr["Rem_EndDate"].ToString()).ToShortDateString();
                      

                        newrow["Category"] = dr["Category"];

                     

                        dtProjectList.Rows.Add(newrow);
                        gr.DataSource = dtProjectList;
                        gr.DataBind();
                        Controls.Add(gr);
                    }

Answer : How to change color in datarow in gridview in Web Part??

you can do a foreach loop after the databind to check each gridviewrow. something like the following:

gr.DataSource = dtProjectList;
gr.DataBind();

foreach (GridViewRow gvr in gr.Rows)
{
   if(gvr.Cells[0].Value.Text == "In Progress")   //Cells[0] (replace with index of status)
  {
       gvr.BackColor = Color.Red;
  }
}
Random Solutions  
 
programming4us programming4us