?
Solved

C# DataGridView setting diffrent color to rows

Posted on 2011-02-19
14
Medium Priority
?
2,302 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 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month9 days, 22 hours left to enroll

762 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