Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Saturday, January 16, 2010 7:43 PM
Hi
Thanks up front
vb.net 2005
I want to have my current row stand out as the current row by virtue of the background color.
I also want my current cell stand out from the row color.
in other words i want to controll the background of the current cell as well as the current row, and have these colors different from each other as wel as the background from the non current cells.
I am getting confused with the notion of the current row vs the selected row.
I have tried messing with the default row style.selectedbackgroungcolor as well as the same for the default cell style, but cant get it to work the way i want.
i can almost get it if i set the selection mode to full row, but then cannot get the current cell to have a different background color.
Thank you all up front.
I will get back to you if i figure it out.
jerry
All replies (6)
Saturday, January 16, 2010 9:40 PM âś…Answered
Thank you all for your help
Here is what in the end solved my problem.
Private Sub OETrucksDataGridView_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles OETrucksDataGridView.CellFormatting
Dim dgv As DataGridView = CType(sender, DataGridView)
If dgv.CurrentRow Is Nothing Then Exit Sub
' get my colors from the grid's DefaultCellStyle
' and RowsDefaultCellStyle
Dim FGAll As Color = dgv.DefaultCellStyle.ForeColor
Dim BGAll As Color = dgv.DefaultCellStyle.BackColor
Dim FGSelRow As Color = dgv.RowsDefaultCellStyle.SelectionForeColor
Dim BGSelRow As Color = dgv.RowsDefaultCellStyle.SelectionBackColor
Dim FGCurCell As Color = dgv.DefaultCellStyle.SelectionForeColor
Dim BGCurCell As Color = dgv.DefaultCellStyle.SelectionBackColor
If dgv.Rows(e.RowIndex).Selected Then
If dgv.CurrentCell.ColumnIndex = e.ColumnIndex Then
' current cell is selected
e.CellStyle.SelectionForeColor = FGCurCell
e.CellStyle.SelectionBackColor = BGCurCell
Else
' current row is selected
e.CellStyle.SelectionForeColor = FGSelRow
e.CellStyle.SelectionBackColor = BGSelRow
End If
Else
' this row is not selected
e.CellStyle.ForeColor = FGAll
e.CellStyle.BackColor = BGAll
End If
End Sub
I abandoned the cell enter/cellleave/rowenter method of accomplishing my goal as it resembled a hack job.
Thanks to Binary coder, I came up with the correct event and eventually the better way of doing it.
Jerry Cic
Saturday, January 16, 2010 8:01 PM
Hi, could you put the code you have?Si la respuesta te ha sido util Marcala como Respuesta o Votala.
Mi Blog: Jtorrecilla
Saturday, January 16, 2010 8:12 PM | 1 vote
I think combining the CellFormatting event with the properties you already found might give you the effects you want. This will let you control what happens to the active row and cell.
Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
If e.RowIndex = DataGridView1.CurrentRow.Index Then
If e.ColumnIndex = DataGridView1.CurrentCell.ColumnIndex Then
e.CellStyle.BackColor = Color.Coral;
Else
e.CellStyle.BackColor = Color.Yellow;
End If
End If
End Sub
Saturday, January 16, 2010 8:12 PM
Sure.
Me.OETrucksDataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect
Private Sub OETrucksDataGridView_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles OETrucksDataGridView.CellEnter
Debug.Print("CellEnter " + e.RowIndex.ToString + "," + e.ColumnIndex.ToString)
Me.OETrucksDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.SelectionBackColor = Color.Teal
'Me.OETrucksDataGridView.CurrentCell.Style.SelectionBackColor = Color.Teal
End Sub
Private Sub OETrucksDataGridView_CellLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles OETrucksDataGridView.CellLeave
Debug.Print("CellLeave " + e.RowIndex.ToString + "," + e.ColumnIndex.ToString)
Me.OETrucksDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.SelectionBackColor = Me.OETrucksDataGridView.RowsDefaultCellStyle.SelectionBackColor
'Me.OETrucksDataGridView.CurrentCell.Style.SelectionBackColor = Me.OETrucksDataGridView.CurrentCell.InheritedStyle.SelectionBackColor
End Sub
If i run this, i get my grid and it comes up with the first row highlighted and the first cell in teal.
As i use the right arrow to go to another row, All works well.
If i am in any column exept the first column when i use the down arrow to get to another row, The first cell in the new row comes up a steal. Also the correct cell comes up as teal.
Maybe my approach is all wrong.
I am so close.
Thanks for your assist
Jerry
Saturday, January 16, 2010 8:25 PM | 1 vote
Hi
Thanks up front
vb.net 2005I want to have my current row stand out as the current row by virtue of the background color.
I also want my current cell stand out from the row color.in other words i want to controll the background of the current cell as well as the current row, and have these colors different from each other as wel as the background from the non current cells.
I am getting confused with the notion of the current row vs the selected row.
I have tried messing with the default row style.selectedbackgroungcolor as well as the same for the default cell style, but cant get it to work the way i want.i can almost get it if i set the selection mode to full row, but then cannot get the current cell to have a different background color.
Thank you all up front.
I will get back to you if i figure it out.jerry
Hello,
do you want to try it please, should solve your problem, thanks:
' Configures the appearance and behavior of a DataGridView control.
Private Sub HighLight_DataGridView()
' Initialize basic DataGridView properties.
DataGridView1.BackgroundColor = Color.LightGray
DataGridView1.BorderStyle = BorderStyle.Fixed3D
' Set property values appropriate for read-only display and
' limited interactivity.
' Set the selection background color for all the cells.
DataGridView1.DefaultCellStyle.SelectionBackColor = Color.White
DataGridView1.DefaultCellStyle.SelectionForeColor = Color.Black
' Set RowHeadersDefaultCellStyle.SelectionBackColor so that its default
' value won't override DataGridView.DefaultCellStyle.SelectionBackColor.
DataGridView1.RowHeadersDefaultCellStyle.SelectionBackColor = Color.Empty
' Set the background color for all rows and for alternating rows.
' The value for alternating rows overrides the value for all rows.
DataGridView1.RowsDefaultCellStyle.BackColor = Color.LightGray
DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.DarkGray
' Set the row and column header styles.
DataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = Color.White
DataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Black
DataGridView1.RowHeadersDefaultCellStyle.BackColor = Color.Black
End Sub
Just Be Humble Malange!
Monday, January 18, 2010 1:48 PM
can u mark as answered so we can move on...Thanks...
All the best for you....
Just Be Humble Malange!