Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 671
  • Last Modified:

DataGridView - changing background of row?

I'm using vb.net and datagridview.
When I click a cell how can I have the background color of that entire row change, and when clicking in another row then the first one would go back to normal and the new one will have its background color change?
0
Jess31
Asked:
Jess31
  • 7
  • 4
  • 4
1 Solution
 
ZhaolaiCommented:
The easiest way is to set the datagridview's SelectionMode property to FullRowSelect.

0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
1.You have an option to make the selection Mode to Full Row Select
2.you can handle cell Click and add this code to change row color

Private Sub MYDataGridView_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles MYDataGridView.CellClick
     If Not IsDBNull(Me.MyDataGridView.Rows(e.RowIndex.Cells(YOurCell).Value) Or Me.MyDataGridView.Rows(e.RowIndex.Cells(YOurCell).Value Then   
           Me.MyDataGridView.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.AliceBlue
     End If
    End Sub

Open in new window

0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
the second line of my code is not necessary
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
ZhaolaiCommented:
To supplement @jtoutou's second method, you need to clear the select before setting the row color. Code here:

    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        DataGridView1.ClearSelection()
        DataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.AliceBlue
    End Sub

Open in new window

0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
Thanks zhaolai ..
0
 
Jess31Author Commented:
Zhaolai:
your code highlights the row nicely, but does not clear the previous row that was highlighted.
0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
Why don't you use our first option ?fullrowselect
and if you want change the selection backcolor
0
 
ZhaolaiCommented:
The FullRowSelect should work for you.
If you want to set to a different color other than the default, set it in the RowsDefaultCellStyle property, either in design time or in run time.

To set it in design time, see the attached screenshot.
To set it in run time, here is the code:
        DataGridView1.RowsDefaultCellStyle.SelectionBackColor = Color.Orange



Screenshot.PNG
0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
Put this in your form load event

With MyDataGridView
            .RowsDefaultCellStyle.SelectionBackColor = Color.Red ' Or What ever color you want
            .RowsDefaultCellStyle.SelectionForeColor = Color.White
End With

Open in new window

0
 
Jess31Author Commented:
jtoutou:
Cause having as row select is confusing in situations where user is entering data into individual cells
0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
But this was your initial request..."background color of that entire row change"
With MyDataGridView
            .SelectionMode = DataGridViewSelectionMode.FullRowSelect
            .RowsDefaultCellStyle.SelectionBackColor = Color.Red ' Or What ever color you want
            .RowsDefaultCellStyle.SelectionForeColor = Color.White
End With
This do it for you ....
Is there any different you want?Plz explain
0
 
Jess31Author Commented:
jtoutou:
yes, I want to highlight the entire row. But using the FullRowSelect I lose the ability to discern which cell i'm on.So I need to be able to change the background color of the current row w/o change the selection mode to FullRowSelect
0
 
ZhaolaiCommented:
Try this (It works for me):
At design time, set the SelectionMode to FullRowSelect. Then add the following code:

    Private Sub DataGridView1_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEnter
        DataGridView1.BeginEdit(True)
    End Sub


0
 
Jess31Author Commented:
This hits it on its head. Thanks!
0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
Ooaaaooo i didn't even think about that....Good job Zhaolai
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 7
  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now