[RESLOVED][vbe 2008] DataGridView button color
Hi,
I am trying to add rows containing a button to a Datagridrow control and would like the button to be red. When I use the code below the button does not turn red but the cell containing the button does. How can I turn the button red? Thanks.
Private Sub AddRow(ByVal Product As String, ByVal Stock As String)
Dim NewRow As DataGridViewRow = New DataGridViewRow
Dim CellButton As DataGridViewCell
CellButton = New DataGridViewButtonCell
CellButton.Value = Stock
CellButton.Style.BackColor = Color.Red
NewRow.Cells.Add(CellButton)
DataGridView1.Rows.Add(NewRow)
DataGridView1.AutoResizeColumns()
End Sub
Re: [vbe 2008] DataGridView button color
You have to set the FlatStyle property of the column. There are four ways at least to get there:
1. Select the grid and expand its task menu using the little arrow at the top, right corner. Select the Edit Columns task. Select your button column and set its FlatStyle property.
2. Select the grid and open the Properties window. If no command links are displayed at the bottom then right-click anywhere and check Commands. Select the Edit Columns command. Select your button column and set its FlatStyle property.
3. Select the grid and open the Properties window. Select the Columns property and click the Browse button next to it. Select your button column and set its FlatStyle property.
4. Open the Properties window. Select your button column from the drop-down list at the top and set its FlatStyle property.
Re: [vbe 2008] DataGridView button color
I used every method you suggest to check I have the Flatstyle set to flat and it is definitely set to Flat but when the code runs it creates 3D style boxes that will not color.
2 Attachment(s)
Re: [vbe 2008] DataGridView button color
I don't know what to tell you. It works. See the attached screen shots. Try just the basics with a new project.
Re: [vbe 2008] DataGridView button color
I see what you are suggesting now. I do not want to permanently turn the cell red but turn it red from my code when required.
CellButton.Style.BackColor = Color.Red
It is this attempt to change the color that does not work.
Re: [vbe 2008] DataGridView button color
Quote:
Originally Posted by DC123
I see what you are suggesting now. I do not want to permanently turn the cell red but turn it red from my code when required.
CellButton.Style.BackColor = Color.Red
It is this attempt to change the color that does not work.
If you have set the FlatStyle of the column to Flat or Popup then that code will work.
2 Attachment(s)
Re: [vbe 2008] DataGridView button color
Take a look at the attached.
Re: [vbe 2008] DataGridView button color
That would be because you're creating the cell yourself instead of letting the grid do it for you. As a result the cell has all the default properties instead of inheriting them from your column. You would only do that if you specifically did NOT want the row to take on the default properties from the grid. Change your AddRow method to this:
vb.net Code:
Dim rowIndex As Integer = Me.DataGridView1.Rows.Add(stock)
Dim row As DataGridViewRow = Me.DataGridView1.Rows(rowIndex)
Dim cell As DataGridViewCell = row.Cells(0)
cell.Style.BackColor = Color.Red
and it will work. Note that that code could also be written in a single line:
vb.net Code:
Me.DataGridView1.Rows(Me.DataGridView1.Rows.Add(stock)).Cells(0).Style.BackColor = Color.Red
Re: [vbe 2008] DataGridView button color