Solved

C# DataGridView setting diffrent color to rows

Posted on 2011-02-19
14
2,279 Views
Last Modified: 2012-05-11
DataGridView setting diffrent color to rows
0
Comment
Question by:amielDorel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

717 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