我想更改RichTextBox的背景颜色,它位于DataGridView的单元格中。
我试过用
Me.dgvPartTracking.Item(columnIndex, rowIndex).Style.BackColor = Color.LightGreen
Me.dgvPartTracking.Item(columnIndex,rowIndex).Style.ForeColor = Color.Black
但结果是只更改了单元格的背景,并且富文本框背景颜色仍然保持白色
输出如下:Sample Result
我用来将RichTextBox分配到DataGridView的方法。
*我使用循环来添加列和行,如下所示
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)
我还没有在这段代码中设置背景颜色,因为我想在之后更改DataGridView中每个单元格的不同背景颜色
我刚刚使用以下方式解决了这个问题:
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
谢谢你回答我@jmcilhinney,我从你的评论中得到了这个想法