Solved

C# DataGridView setting diffrent color to rows

Posted on 2011-02-19
14
2,239 Views
Last Modified: 2012-05-11
DataGridView setting diffrent color to rows
0
Comment
Question by:amielDorel
  • 5
  • 4
  • 3
  • +1
14 Comments
 
LVL 5

Accepted Solution

by:
sameer_goyal earned 500 total points
ID: 34933472
You can use AlternateRowStyle property of DataGridView or DataGrid to do this...
0
 
LVL 13

Expert Comment

by:Naman Goel
ID: 34933473
use following property of DefaultCellStyle.BackColor property of DataGridViewRow

like following

dataGridView.CurrentRow.DefaultCellStyle.BackColor = System.Drawing.Color.Aqua;

also refer to following links for more info :

http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/31ef832b-2fe2-4554-bf0c-8495f9e85ede/

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_22987713.html

0
 
LVL 5

Expert Comment

by:sameer_goyal
ID: 34933481
Additional Info for the above query: Using AlternateRowStyle will enable you to use only 2 different colors for rows. If your intentions/requirements is to be able to color every row with a new color, you will have to create a logic and apply that in the Row_Created event of the DataGridView
0
 
LVL 13

Expert Comment

by:Naman Goel
ID: 34933487
agreed with sameer...
0
 

Author Comment

by:amielDorel
ID: 34934104

Thank you for your reply, I do need to set some rows color base on specific data, it looks like the
 Row_Created event is what I need to look into. Do you know of any helpfull links I can use.
0
 
LVL 5

Expert Comment

by:sameer_goyal
ID: 34936215
Nothing beats MSDN for help on .NET :)
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcreated.aspx

In case you need help in implementing, let me know and i will try to help.
0
 
LVL 2

Expert Comment

by:chTeja
ID: 34941753
Hi you can use the ItemDataBound property for the DataGrid to set the differnt colours  as per the condition

public virtual void GridView2_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            System.Web.UI.WebControls.ListItemType itemType = (System.Web.UI.WebControls.ListItemType)e.Item.ItemType;
            class dt = (e.Item.DataItem) as class;

            if (e.Item.ItemType == ListItemType.Header)
            {
                e.Item.BackColor = System.Drawing.Color.Black;
                e.Item.ForeColor = System.Drawing.Color.White;
            }
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                e.Item.ForeColor = System.Drawing.Color.SaddleBrown;
                if (dt.estimation.ToString() == "8")
                {
                    e.Item.BackColor = System.Drawing.Color.RosyBrown;
                    
                }
                else if (dt.estimation.ToString() == "10")
                {
                    e.Item.BackColor = System.Drawing.Color.Tomato;
                }
                else
                {
                    e.Item.BackColor = System.Drawing.Color.RoyalBlue;
                }
            }

Open in new window


please let me know your feed back.. weather this will be help full or not

Thanks
Sri Narayana Rao.D
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:amielDorel
ID: 34944510
I don't see _ItemDataBound  as a member of the DataGrid, unfortunately I can't use any of the code.
 
0
 
LVL 5

Expert Comment

by:sameer_goyal
ID: 34944765
ItemDataBound is an event for DataGrid and not for DataGridView. For DataGridView, you have to use RowCreated event. I believe, the MSDN link I provided above should have helped you.
0
 

Author Comment

by:amielDorel
ID: 34944915

Unfortunately, there is no RowCreated event for the DataGridView,  (windows form application)
0
 
LVL 5

Expert Comment

by:sameer_goyal
ID: 34944985
Ok so it was Windows App you are working with. Try 'RowsAdded' event then,
0
 

Author Comment

by:amielDorel
ID: 34946623
Please provide an example to work in a windows App. (similar to the one provided before)
0
 
LVL 2

Expert Comment

by:chTeja
ID: 34948774
hey,

sorry for the misleading .. i have named my datagrid as as GridView1

here is the following aspx code..
 <asp:DataGrid ID="dgView" runat="server" OnItemDataBound="GridView2_ItemDataBound" >
        </asp:DataGrid>

Open in new window

paste this code in your aspx.cs page
public virtual void GridView2_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            System.Web.UI.WebControls.ListItemType itemType = (System.Web.UI.WebControls.ListItemType)e.Item.ItemType;
            yourownclass dt = (e.Item.DataItem) as yourownclass;

            if (e.Item.ItemType == ListItemType.Header)
            {
                e.Item.BackColor = System.Drawing.Color.Black;
                e.Item.ForeColor = System.Drawing.Color.White;
            }
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                e.Item.ForeColor = System.Drawing.Color.SaddleBrown;
                   // assume that you have the estimation column in your datasource which will have
                   // different values like 8,10,5 based on estimation i am setting the back colour as differnt 
                   //one
                if (dt.estimation.ToString() == "8") 
                {
                    e.Item.BackColor = System.Drawing.Color.RosyBrown;
                    
                }
                else if (dt.estimation.ToString() == "10")
                {
                    e.Item.BackColor = System.Drawing.Color.Tomato;
                }
                else
                {
                    e.Item.BackColor = System.Drawing.Color.RoyalBlue;
                }
            }

Open in new window

0
 
LVL 13

Expert Comment

by:Naman Goel
ID: 34948972
Here is the solution

Here I am using dataGridView1_CellFormatting event for this

My grid contains and ID Column which contains serial number as its values

I am trying to represent even number row with black background color and odd number with smoke white.

You can use this logic to change background color at your own choice.




private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex == dataGridView1.Columns["ID"].Index)
            {
                if (e.Value!=null && e.Value.GetType()== typeof(int))
                {
                    int value = int.Parse(e.Value.ToString());
                    if (value % 2 == 0)
                    {
                        dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.White;
                        dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Black;
                    }
                    else
                    {
                        dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.DarkGreen;
                        dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.WhiteSmoke;
                    }
                }
            }
        }

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now