How to Change BackColor of Cell in DataGridView?

0

I have this arrangement:

 Color[] colores = { System.Drawing.Color.White,
                     System.Drawing.Color.WhiteSmoke,
                     System.Drawing.Color.Yellow };

and on the other hand, I have a DataGridView that will always contain the same number of rows as elements the array colors. what I need is to change is the color of the cells in the first column called Color by its respective in the arrangement ... something like this:

  COLOR                  NOMBRE                                     

BackGround White          WHITE                         

BackGround WhiteSmoke     WHITESMOKE                      

BackGround Yellow         YELLOW                       
    
asked by Efrain Mejias C 15.05.2017 в 22:01
source

2 answers

0

So he does it perfectly

   Color[] colores = {
            System.Drawing.Color.White , System.Drawing.Color.WhiteSmoke, System.Drawing.Color.DimGray,
            System.Drawing.Color.Gray, System.Drawing.Color.DarkGray,  System.Drawing.Color.Silver, System.Drawing.Color.LightGray,
            System.Drawing.Color.Gainsboro, System.Drawing.Color.Maroon,  System.Drawing.Color.DarkRed, System.Drawing.Color.Red,
            System.Drawing.Color.Brown, System.Drawing.Color.Firebrick,System.Drawing.Color.IndianRed, System.Drawing.Color.Snow,
            System.Drawing.Color.LightCoral, System.Drawing.Color.RosyBrown,System.Drawing.Color.MistyRose, System.Drawing.Color.Salmon,
            System.Drawing.Color.Tomato, System.Drawing.Color.DarkSalmon,System.Drawing.Color.Coral, System.Drawing.Color.OrangeRed,
            System.Drawing.Color.LightSalmon,System.Drawing.Color.Sienna, System.Drawing.Color.SeaShell, System.Drawing.Color.Chocolate,
            System.Drawing.Color.SaddleBrown, System.Drawing.Color.SandyBrown, System.Drawing.Color.PeachPuff, System.Drawing.Color.Peru,
            System.Drawing.Color.Linen, System.Drawing.Color.Bisque, System.Drawing.Color.DarkOrange, System.Drawing.Color.BurlyWood,
            System.Drawing.Color.Tan, System.Drawing.Color.AntiqueWhite,System.Drawing.Color.NavajoWhite, System.Drawing.Color.BlanchedAlmond,
            System.Drawing.Color.PapayaWhip, System.Drawing.Color.Moccasin,  System.Drawing.Color.Orange, System.Drawing.Color.Wheat,
            System.Drawing.Color.OldLace,
        };
 private void DataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (this.DataGridView1.Columns[e.ColumnIndex].Name == "Color")
                e.CellStyle.BackColor = colores[e.RowIndex];  
        }

    
answered by 15.05.2017 / 22:41
source
2
  

Modified code of this accepted response in Stack Overflow in English :

One option may be the following:

int colorIncr = 0; // Este valor se usará para intercambiar los colores.

// Recorre las filas de tu DataGridView.
for (int i = 0; i < dtgv1.Rows.Count)
{
    // Establecer color a la fila.
    dtgv1.Rows[i].DefaultCellStyle.ForeColor = colores[colorIncr];

    // Verificar que valor de incremento no sea mayor la
    // cantidad de datos del arreglo de colores.
    if (colorIncr >= colores.Length)
    {
       // Valor original.
       colorIncr = 0;
    }
}
    
answered by 15.05.2017 в 22:15