Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gridview highlighting current row

Is there a built-in method of highlighting the currently selected row in a gridview?

Each row in my gridview has a button (via a buttonField). When the user presses this button, the background color changes...I do it like this:

Protected Sub gvTransferOwner_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvTransferOwner.RowCommand
    If e.CommandName = "Select" Then
        Dim index As Integer = Convert.ToInt32(e.CommandArgument)
        Dim selectedRow As GridViewRow = gvTransferOwner.Rows(index)
        selectedRow.Style.Add("background-color", "#ffcccc")
    End If
End Sub

This highlights the row, but once the user presses the button in another row, it still retains that color in all previously-pressed rows.

Is there a way so that only one row at a time(the currently selected row) is highlighted?

Thanks

like image 333
SkyeBoniwell Avatar asked Mar 19 '26 21:03

SkyeBoniwell


2 Answers

If you use a global variable to store the index of the row that is being selected, you can change that row back to the original color whenever a new row is selected.

Dim previousSelected As Integer 'global variable to store the last selected index
Protected Sub gvTransferOwner_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvTransferOwner.RowCommand
    If e.CommandName = "Select" Then
        Dim index As Integer = Convert.ToInt32(e.CommandArgument)
        Dim selectedRow As GridViewRow = gvTransferOwner.Rows(previousSelected)
        selectedRow.Style.Add("background-color", "#ffffff") 'change it back to original color
        selectedRow = gvTransferOwner.Rows(index)
        selectedRow.Style.Add("background-color", "#ffcccc") 'change the color of the new row
        previousSelected = index
    End If
End Sub
like image 156
jonhopkins Avatar answered Mar 22 '26 11:03

jonhopkins


The gridview has a SelectedIndexChanged event as well as has a GridView.SelectedRow Property which you can use in conjunction with @jonhopkins' answer.

like image 25
Clarice Bouwer Avatar answered Mar 22 '26 11:03

Clarice Bouwer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!