Tuesday, 10 May 2016

vb.net - How To Change Background Color of RichTextBox After Assigning into DataGridView



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


Answer



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


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...