Click here to Skip to main content
14,664,481 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have a grid view, and I want that which row i click to change its background color to red. but I have a problem, i click a row and the color changes to red, then when i click a different row it also changes its back color but the previews one stays red. I want that the only selected row to be red.
my code looks like this:

i = MyGrd.SelectedCells(0).RowIndex
MyGrd.Rows(i).DefaultCellStyle.BackColor = Color.Red
Posted

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Refer- HOW TO CHANGE ( SET ) GRIDVIEW SELECTED ROW COLOR IN ASP.NET[^]

You can easily change the Row Background colour inside SlectedIndexChanged Event like below.
Protected Sub OnSelectedIndexChanged(sender As Object, e As EventArgs)
    For Each row As GridViewRow In GridView1.Rows
        If row.RowIndex = GridView1.SelectedIndex Then
            row.BackColor = ColorTranslator.FromHtml("#A1DCF2")
        Else
            row.BackColor = ColorTranslator.FromHtml("#FFFFFF")
        End If
    Next
End Sub

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100