Results 1 to 6 of 6

Thread: Get the BackColor from the selected DataGridView row when clicked.

  1. #1

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    313

    Get the BackColor from the selected DataGridView row when clicked.

    Hello!
    I have a DataGridView and a ListBox.
    My DGV rows are painted in 4 different colors.

    My goal is, when I click on a row in my DGV, I want my ListBox to change it's BackColor based on the row I selected on the DGV.

    This is what I've tried so far and I still can't make it happen.

    vb.net Code:
    1. Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
    2. ListBox1.BackColor = DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(1).Style.BackColor
    3. End Sub

    If I debug this code, I found that ListBox1.BackColor gets the value of: "{Name=0, ARGB=(0, 0, 0, 0)}"

    Thank you, I'll appreciate any help.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,843

    Re: Get the BackColor from the selected DataGridView row when clicked.

    One possible issue is that DataGridView1.CurrentRow.Index might not return the correct value (as clicking a cell might not set CurrentRow before the event is fired), so you should be making use of the event parameters instead - in this case, use: e.RowIndex

    Another possible issue is how the colour of the DGV rows is set. As you can set that by a variety of properties (such as the DefaultCellStyle of the row, etc), you should make sure that you read the same property that you set in the first place, as they don't automatically set the other properties you could have used.

  3. #3
    Junior Member
    Join Date
    Jan 2022
    Posts
    21

    Re: Get the BackColor from the selected DataGridView row when clicked.

    your code seems redundant, you are not actually getting the rows color, you are getting a cell color from the second column of the roundabout currentrow. If you want to use the cell color, you may as well use the currentcell property
    Code:
    ListBox1.BackColor = DataGridView1.CurrentCell.Style.BackColor
    Similarly you can use the currentrow property
    Code:
    ListBox1.BackColor = DataGridView1.CurrentRow.DefaultCellStyle.BackColor

  4. #4

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    313

    Re: Get the BackColor from the selected DataGridView row when clicked.

    Sounds very logical to me.
    As 2 of the 4 colours of my DGV rows were set using the DefaultCellStyle property of the row, I change the property and it worked, but only for 2 of the 4 colours.
    The other two colours were set with the AlternatingRowsDefaultCellStyle.BackColor property, and these don't work, ListBox1.Backcolor gets the value of "[Empty]".

    I'm using this code:
    vb.net Code:
    1. If DataGridView1.CurrentRow.DefaultCellStyle.BackColor.ToString <> "[Empty]" Then
    2.    ListBox1.BackColor = DataGridView1.CurrentRow.DefaultCellStyle.BackColor
    3. ElseIf DataGridView1.CurrentRow.DefaultCellStyle.BackColor.ToString = "[Empty]" Then
    4.    ListBox1.BackColor = DataGridView1.AlternatingRowsDefaultCellStyle.BackColor
    5. End If
    Last edited by Spybot; Jul 3rd, 2022 at 09:10 PM.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,855

    Re: Get the BackColor from the selected DataGridView row when clicked.

    Just test if AlternatingRowsDefaultCellStyle.BackColor is nothing, if so use DefaultCellStyle

  6. #6

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    313

    Re: Get the BackColor from the selected DataGridView row when clicked.

    Ok, I changed my code to:
    vb.net Code:
    1. Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
    2. ListBox1.BackColor = DataGridView1.CurrentRow.DefaultCellStyle.BackColor
    3. End Sub
    And still only works for the first 2 colours.

    Name:  g3690.jpg
Views: 43
Size:  48.4 KB

    Name:  g3700.jpg
Views: 35
Size:  44.5 KB

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