Lorenzo Belfanti
Lorenzo Belfanti

Reputation: 1247

Display to a user the selection of multiple colors in a cell of a DataGridView

I have two DataGridView, the first containing a list of users the other containing colors.

First DataGridView (Users)

The First DataGridView

The second DataGridView (Colors)

DataGridView that contains colors

By selecting a cell from the first DataGridView, the program allows you to associate a color with a user. The association takes place using the right mouse button and selecting the color.

After selection, the user's cell becomes colored, of the selected color, using the BackGroundColorproperty.

The problem

The problem now is that I have to allow the user to associate multiple colors with a user, so how can I visualize that the cell has been associated with multiple colors?

Do you have any graphic idea of how to accomplish this? Unfortunately, you can not associate more colors in a Cell, the only thing you can do is use the gradient but it does not matter to me.

Upvotes: 2

Views: 67

Answers (1)

Jegan
Jegan

Reputation: 1237

You could do this by accessing the 'CellPainting' event on the DataGridView.

    private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
        var temp = sender as DataGridView;

        if (temp.ColumnCount > 0 && temp.RowCount > 0)
        {
            // get the first cell at (0, 0)
            var cellposition = dataGridView1.GetCellDisplayRectangle(0, 0, false);
            var xStart = cellposition.X;
            var yStart = cellposition.Y;
            var xEnd = xStart + cellposition.Width / 2;
            var yEnd = yStart + cellposition.Height;
            for (int i = yStart; i < yEnd; ++i)
            {
                e.Graphics.DrawLine(new Pen(Color.Black, 1), new Point(xStart, i), new Point(xEnd, i));
            }
        }
    }

This example code shows you how to paint on the Cell 1 (i.e. Column 0, Row 0), you can use this example to extend to further cells.

Upvotes: 1

Related Questions