I want to change the value & its backcolor of cell.
On Button1_Click,I m adding records to the Gridview.
On Button3_Click,I want to chnage the value of cell & its backcolor.

Suppose two Rows & two columns are there in Grdiview
1 10
2 20

Suppose I want to change the value of item(Row=2,Colomn=2) to 100. I change the value but i m not ablt to change the backcolor.Can somebody tell me?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try

            Dim row0 As String() = {TextBox1.Text, TextBox2.Text}
            DataGridView1.Rows.Add(row0)
            row0 = Nothing

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub


 Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Try

            DataGridView1.Item(1, 1).Value = 100
           'What to write to chnage the cell color

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

Dani AI

Generated

— changing the cell Value is fine; the background color is controlled by the cell style, and selection/column/row defaults can hide changes. Set the cell's Style.BackColor (and also Style.SelectionBackColor so selection doesn't overwrite it), and remember DataGridView indexes are zero-based.

Example (VB.NET):

Dim r As Integer = 1
Dim c As Integer = 1

DataGridView1.Rows(r).Cells(c).Value = 100

With DataGridView1.Rows(r).Cells(c).Style
    .BackColor = Color.LightGreen
    .SelectionBackColor = Color.LightGreen
End With

DataGridView1.ClearSelection()

Common reasons a color change is not visible:

  • The cell is selected and the grid's selection color covers the BackColor (set SelectionBackColor or call ClearSelection).
  • A higher-level DefaultCellStyle (column/row/DataGridView) or a CellFormatting event is resetting styles; cell.Style has highest priority and should be applied after binding or inside CellFormatting if the grid is rebound.
  • The style is being overwritten during repaint events or immediately after re-adding rows; apply styles after any data refresh.

Authoritative reference: see the DataGridView cell style properties (DataGridViewCell.Style and DataGridViewCellStyle.BackColor) for behavior and precedence.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.