Click here to Skip to main content
15,176,561 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

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

1 solution

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.
VB
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900