1
votes

I would like to change the background color of RichTextBox which is in a cell of DataGridView.

I have tried use

Me.dgvPartTracking.Item(columnIndex, rowIndex).Style.BackColor = Color.LightGreen

Me.dgvPartTracking.Item(columnIndex, rowIndex).Style.ForeColor = Color.Black

but the result is only the background of cell is changed, and the RichTextBox beackground color is still remaining in white color

The output is as below: Sample Result

The Method I used to assign the RichTextBox into DataGridView.

*I use looping to add the columns and rows as below

        Dim Col As New DataGridViewRichTextBoxColumn
        Col.Name = "schedule" & columnCount
        Col.HeaderText = "" & columnCount
        Col.DefaultCellStyle.WrapMode = Windows.Forms.DataGridViewTriState.True
        Col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft
        Col.Width = 195
        Col.ReadOnly = True
        Col.SortMode = Windows.Forms.DataGridViewColumnSortMode.NotSortable
        Col.Resizable = Windows.Forms.DataGridViewTriState.True
        Col.AutoSizeMode = Windows.Forms.DataGridViewAutoSizeColumnMode.NotSet
        Col.Visible = True
        Me.dgvPartTracking.Columns.Add(Col)
        Me.dgvPartTracking.Rows.Add(1)

I didn't set the background color in this code yet, because I would like to change the different background colors for each cells in DataGridView afterward

1
The Item property of a DataGridView returns a DataGridViewCell, not a RichTextBox. You need to actually access the control in the cell but, if you have done things properly, such a control should only exist while a cell in that column is being edited. We don't know enough about your implementation to know how to access that control but you should, given that you put it there.jmcilhinney
Because that's exactly what you're doing. You changed the back color of the cell, not the RichTextBox. You never showed how you added the RichTextBox but whenever you do so, set its back color to whatever color you want.41686d6564
I have just updated the postqwerty

1 Answers

0
votes

I have just solved this problem by using the way as below:

Dim cell As New DataGridViewRichTextBoxCell
cell.setBackColor(Color.LightGreen)
Me.dgvPartTracking.Item(columnIndex, rowIndex) = cell
Me.dgvPartTracking.Item(columnIndex, rowIndex).Style.BackColor = Color.LightGreen
Me.dgvPartTracking.Item(columnIndex, rowIndex).Style.ForeColor = Color.Black

Thank You for answering to me @jmcilhinney, I got the idea from your comment