0
votes

Below is the code that successfully works for changing the background color of a cell in VBA Excel.

Sub ColorChange()
    For i = 1 To 4
        Cells(i, 2).Interior.ColorIndex = i + 2
    Next i
End Sub

Can anyone help in getting how to change the DataGridView cells background color through this color index approach in VB.Net

2
VBA and VB.NET are two entirely different beasts. You will need to read the documentation for the DataGridView.Sam Axe

2 Answers

0
votes

You can change the background colour of a cell by setting dgv.Rows(x).Cells(y).Style.BackColor, or in your case, in your loop, you do:

    dgv.Rows(i).Cells(2).Style.BackColor = Color.Red

Where dgv is your DataGridView in both respective cases. (And you change the colour to what you want, such as Color.Blue, etc.)

0
votes

I needed to change the color constantly in a loop. So tried it using FromArgb and it worked successfully.

DataGridView1.Rows(i).Cells(j).Style.BackColor = Color.FromArgb(a, b, c)

Thanks for the help