-
Jul 3rd, 2022, 01:57 PM
#1
Thread Starter
Hyperactive Member
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:
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
ListBox1.BackColor = DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(1).Style.BackColor
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.
-
Jul 3rd, 2022, 02:32 PM
#2
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.
-
Jul 3rd, 2022, 05:27 PM
#3
Addicted Member
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
-
Jul 3rd, 2022, 09:07 PM
#4
Thread Starter
Hyperactive Member
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:
If DataGridView1.CurrentRow.DefaultCellStyle.BackColor.ToString <> "[Empty]" Then
ListBox1.BackColor = DataGridView1.CurrentRow.DefaultCellStyle.BackColor
ElseIf DataGridView1.CurrentRow.DefaultCellStyle.BackColor.ToString = "[Empty]" Then
ListBox1.BackColor = DataGridView1.AlternatingRowsDefaultCellStyle.BackColor
End If
Last edited by Spybot; Jul 3rd, 2022 at 09:10 PM.
-
Jul 3rd, 2022, 09:14 PM
#5
Re: Get the BackColor from the selected DataGridView row when clicked.
Just test if AlternatingRowsDefaultCellStyle.BackColor is nothing, if so use DefaultCellStyle
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 4th, 2022, 12:56 AM
#6
Thread Starter
Hyperactive Member
Re: Get the BackColor from the selected DataGridView row when clicked.
Ok, I changed my code to:
vb.net Code:
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
ListBox1.BackColor = DataGridView1.CurrentRow.DefaultCellStyle.BackColor
End Sub
And still only works for the first 2 colours.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|