[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

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

Posted on 2010-01-04
9
Medium Priority
?
1,106 Views
Last Modified: 2013-12-17
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..
SPList Projectlist = web.Lists["ListA"];
                SPQuery queryProjectList = new SPQuery();
                queryProjectList.ViewFields = "<FieldRef Name=\"Title\"/>";

                queryProjectList.Query = "<OrderBy><FieldRef Name=\"Title\"/>"<FieldRef Name='Category' Ascending='False' /></OrderBy>";
                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);
                    }

Open in new window

0
Comment
Question by:guessdip
9 Comments
 
LVL 4

Accepted Solution

by:
krys2fer earned 1000 total points
ID: 26176827
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;
  }
}
0
 
LVL 5

Assisted Solution

by:manojce
manojce earned 1000 total points
ID: 26177451
hi guessdip,

I think use RowDataBound method as per below

Page_Load()
{
// Bind grid here
}


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.Cells[0].Text== "In Progress")  
                {
                    e.Row.BackColor = Color.Red;
                }
            }
        }

In this method no need to run extra For/ ForEach loop.

I think this is best solution for Color change with out lopping.

Happy Coding...
For More details check http://sharepointbuild.blogspot.com/
0
 
LVL 12

Expert Comment

by:mccarthybri
ID: 26177919
try this below it hasnt failed me yet.  it works in a CEWP.  
How-to-add-color-to-columns-in-s.doc
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 5

Expert Comment

by:sureshchsahu
ID: 26178460
Look for the below link it has given clear explanation for the question you have asked for

http://www.simple-talk.com/dotnet/asp.net/take-row-level-control-of-your-gridview/
0
 
LVL 5

Expert Comment

by:sureshchsahu
ID: 26178469
0
 
LVL 5

Expert Comment

by:sureshchsahu
ID: 26178477
0
 
LVL 19

Expert Comment

by:MsShadow
ID: 26178714
I would suggest Mccarthybri's solution. That way you can apply it to other lists as well, change the color of needed without having to redeploy your webpart. If you created a webpart just for the color coding, then that is a step you can pass as well as his code can apply it to any standard list in your SharePoint site.
0
 

Author Comment

by:guessdip
ID: 26185226
Hii,
 Thank you so so much for respond, i am trying apply this logic and definetly i can do.

Msshadow:
I need to use Custom  Webpart becuase i have to apply some logic to display records and i also need to use sorting,filtering and also i need to inline edit function.

First time i am using Gridview so i can ask another question for sorting, filtering and Edit function.
0
 

Author Closing Comment

by:guessdip
ID: 31672717
Thank you dude, You both are correct. i am able to change color . I have another question for gridview, hope you will help me..
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The PowerShell Core 6.0 of .NET release is just the beginning. The upcoming PowerShell Core 6.1 would have artificial intelligence and internet of things capabilities. So many things to look forward to in the upcoming release.
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Free Data Recovery software is an advanced solution from Kernel Tools to recover data and files such as documents, emails, database, media and pictures, etc. It supports recovery from physical & logical drive after a hard disk crash, accidental/inte…

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question