Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
using c# winform 4.0

The datagridview has several columns including column ItemID
The datagridview has been populated with several rows.
Some of the rows have the same ItemID value.
I would like the rows with the same ItemID to be colour coded.

For example:
rows 2 and 5 may have the same ItemID. These two rows should have one colour
rows 6 and 7 may have the same ItemID. These two rows should have a one colour. i.e. different to the rows 2 and 5
...

How is this done please?
Posted

1 solution

The CellFormatting event of DataGridView can be used for this purpose as shown below:
C#
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    DataGridView DataGridView1 = new DataGridView();
    Button ButtonUpdate = new Button();
    BindingSource BindingSource1 = new BindingSource();
    DataTable DataTable1 = new DataTable();
    Color[] colorTable;
    private void Form1_Load(object sender, EventArgs e)
    {
        colorTable = new Color[]{
               Color.Blue, Color.Red, Color.Green,
               Color.Pink, Color.Plum, Color.Cyan};
        DataTable1.Columns.Add("ItemId", typeof(int), null);
        {
            DataTable1.Rows.Add(1);
            DataTable1.Rows.Add(2);
            DataTable1.Rows.Add(2);
            DataTable1.Rows.Add(5);
            DataTable1.Rows.Add(5);
            DataTable1.Rows.Add(6);
            DataTable1.Rows.Add(6);
            DataTable1.Rows.Add(7);
            DataTable1.Rows.Add(7);
        }
        BindingSource1.DataSource = DataTable1;
        DataGridView1.DataSource = BindingSource1;

        DataGridView1.Dock = DockStyle.Fill;
        this.Controls.Add(DataGridView1);
        DataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(DataGridView1_CellFormatting);

    }

    void DataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        if (e.ColumnIndex == 0)
        {
            try
            {
                int val = (int)DataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
                e.CellStyle.BackColor = colorTable[val % 6];
            }
            catch
            {
                return;
            }
        }
    }
}
 
Share this answer
 
Comments
arkiboys 25-Apr-12 7:56am    
Hi,
I do not see how your code identifies the rows which have the same column value. Can you explain please?
Thanks
VJ Reddy 25-Apr-12 8:06am    
In the CellFormatting event the contents of the First Column of the concerned is obtained as val. Then remainder of the val is calculated using val % 6. So, for any value we will get values from 0 to 5 as remainder. Here, I have used only few colors in the color table. Depending upon the range of numbers in your column, populate the color table, and find out the remainder. If the value 5 is repeated in 3 rows, we will get the same remainer for all these rows and then same color from the color table will applied as the back color in CellFormatting event.
arkiboys 25-Apr-12 8:13am    
Hi, I still do not understand this...
Because I want only to colour the rows which have the same values in say columnX.
Your code does not seem to be doing that.
Please elaborate.
Thanks
VJ Reddy 25-Apr-12 8:25am    
Have you run the sample?
I have entered the values 5, 6, 7 etc. which are repeated, for which you get the same color, respectively. 5 % 6 = 5, 6 % 6 =0, 7 % 6 = 1, So, for all rows with value of 5, in the ItemId, the colorTable[5] will be applied as back color. Then for all rows with value of 6 in the ItemId, colorTable[0] will be applied as back color similarly for other values the back color is applied.
I have run the above sample and it works.
VJ Reddy 25-Apr-12 8:56am    
Hope, you understood the solution.
I will be happy if it works for you.
Thank you for your response.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900