Solved

C# DataGridView setting diffrent color to rows

Posted on 2011-02-19
14
2,228 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

759 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

22 Experts available now in Live!

Get 1:1 Help Now