Vb.net 分配到DataGridView后如何更改RichTextBox的背景色
我想更改DataGridView单元格中RichTextBox的背景色 我试过使用Vb.net 分配到DataGridView后如何更改RichTextBox的背景色,vb.net,datagridview,richtextbox,Vb.net,Datagridview,Richtextbox,我想更改DataGridView单元格中RichTextBox的背景色 我试过使用 Me.dgvPartTracking.Item(columnIndex, rowIndex).Style.BackColor = Color.LightGreen Me.dgvPartTracking.Item(columnIndex,rowIndex).Style.ForeColor=Color.Black 但结果只是改变了单元格的背景,RichTextBox beackground颜色仍保留为白色 输出如下
Me.dgvPartTracking.Item(columnIndex, rowIndex).Style.BackColor = Color.LightGreen
Me.dgvPartTracking.Item(columnIndex,rowIndex).Style.ForeColor=Color.Black
但结果只是改变了单元格的背景,RichTextBox beackground颜色仍保留为白色
输出如下:
我用来将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,我从您的评论中得到了这个想法我刚刚通过以下方式解决了这个问题:
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回复我,我从您的评论中了解到了这个想法,
DataGridView
的Item
属性返回一个DataGridViewCell
,而不是RichTextBox
。您需要实际访问单元格中的控件,但如果操作正确,则只有在编辑该列中的单元格时,该控件才应该存在。我们对您的实现知之甚少,不知道如何访问该控件,但您应该这样做,因为这正是您正在做的。您更改了单元格的背景颜色,而不是RichTextBox。您从未演示过如何添加RichTextBox,但每当您这样做时,请将其背景颜色设置为您想要的任何颜色。我刚刚更新了postTheDataGridView
的Item
属性,它返回的是DataGridViewCell
,而不是RichTextBox
。您需要实际访问单元格中的控件,但如果操作正确,则只有在编辑该列中的单元格时,该控件才应该存在。我们对您的实现知之甚少,不知道如何访问该控件,但您应该这样做,因为这正是您正在做的。您更改了单元格的背景颜色,而不是RichTextBox。你从来没有展示过你是如何添加RichTextBox的,但是每当你这样做的时候,把它的背景颜色设置成你想要的任何颜色。我刚刚更新了这篇文章