Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi I'm new in vb and I want to change row and column backcolor for active cell but my code not work well 
:


What I have tried:

Dim col_index As Byte
Dim row_index As Byte
Private Sub dgv1_CellEnter_1(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv1.CellEnter
    Try
        If dgv1.CurrentCell.ColumnIndex > 0 Then
            TextBox1.Text = dgv1.CurrentRow.Cells.Item("stu_sery").Value
            index = dgv1.CurrentRow.Index
            ComboBox1.SelectedIndex = dgv1.CurrentCell.ColumnIndex - 1
            dgv1.Columns(dgv1.CurrentCell.ColumnIndex).DefaultCellStyle.BackColor = Color.Aqua
            dgv1.Rows(dgv1.CurrentCell.RowIndex).DefaultCellStyle.BackColor = Color.Aqua
            dgv1.Columns(col_index).DefaultCellStyle.BackColor= Color.White
            dgv1.Rows(row_index).DefaultCellStyle.BackColor = Color.White

            row_index = dgv1.CurrentCell.RowIndex
            col_index = dgv1.CurrentCell.ColumnIndex
        End If
    Catch ex As Exception

    End Try
End Sub
Posted
Updated 21-Jan-18 13:33pm
Comments
Ram Nunna 19-Jan-18 5:59am    
What is the error you are getting?
Taha Ahmed 19-Jan-18 6:39am    
When I change cell the previous cell row and column backcolor be white also my new active cell column

1 solution

If you change the background color of a cell within the CellEnter event handler the event is fired when you enter the cell. This will be maintained until you reload the grid or until you change the color, either of the following will work;
a) Within your CellEnter Event Handler loop through all the rows & set the color to default, with the exception of the current row
Bad Way - unnecessary loop
b) Add a CellLeave Event Handler. Set the cell color back to default within this event handler
Correct Way

Refer to the following MSDN Article for example code; DataGridView.CellEnter Event (System.Windows.Forms)[^]

Kind Regards
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900