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
Solved

C# DataGridView setting diffrent color to rows

Posted on 2011-02-19
14
2,257 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

840 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