Results 1 to 11 of 11

Thread: [RESLOVED][vbe 2008] DataGridView button color

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    81

    [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
    Last edited by DC123; Jan 4th, 2009 at 05:49 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [vbe 2008]

    First up, please provide thread titles that describe the topic of the thread in future.

    As for your problem, you can't change the colour of the Button as long as it's drawn using visual styles. You need to set the FlatStyle property of the column to Flat or Popup. You will lose the Button-esque appearance as a result.

    The reason is that there actually is no Button control, but rather just a picture of a Button created by a ButtonRenderer, which ignores cell properties if visual styles are in effect. You may be able to change this behaviour if you inherit the class and write your own code but I've looked and it doesn't appear it would be easy at all.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    81

    Re: [vbe 2008]

    Quote Originally Posted by jmcilhinney
    First up, please provide thread titles that describe the topic of the thread in future.
    <sarcasm>
    I find it creates an air of mystery and intrigue if the title is left blank.
    </sarcasm>

    How do I set the Flatstyle property to Flat. I tried the Flatstyle property of the Columns collection of the DataGridView control and it did not work. I still draws system style 3D buttons.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    81

    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.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Attached Images Attached Images   
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    81

    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.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    81

    Re: [vbe 2008] DataGridView button color

    Take a look at the attached.
    Attached Images Attached Images   

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Dim rowIndex As Integer = Me.DataGridView1.Rows.Add(stock)
    2. Dim row As DataGridViewRow = Me.DataGridView1.Rows(rowIndex)
    3. Dim cell As DataGridViewCell = row.Cells(0)
    4.  
    5. cell.Style.BackColor = Color.Red
    and it will work. Note that that code could also be written in a single line:
    vb.net Code:
    1. Me.DataGridView1.Rows(Me.DataGridView1.Rows.Add(stock)).Cells(0).Style.BackColor = Color.Red
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    81

    Re: [vbe 2008] DataGridView button color

    Thanks that works.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width