gridview突出显示当前行

时间:2012-11-26 15:24:55

标签: asp.net .net vb.net

是否有内置方法突出显示gridview中当前选定的行?

我的gridview中的每一行都有一个按钮(通过buttonField)。当用户按下此按钮时,背景颜色会发生变化......我这样做:

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

这会突出显示该行,但是一旦用户按下另一行中的按钮,它仍会在所有先前按下的行中保留该颜色。

是否有一种方法可以一次只显示一行(当前选定的行)?

由于

5 个答案:

答案 0 :(得分:3)

如果使用全局变量来存储所选行的索引,则只要选择了新行,就可以将该行更改回原始颜色。

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

答案 1 :(得分:3)

gridview有一个SelectedIndexChanged事件,还有一个GridView.SelectedRow Property,您可以将其与@jonhopkins的答案结合使用。

答案 2 :(得分:2)

我在SelectedIndexChanged事件中这样做,它对我有用。

GridView1.Rows[GridView1.SelectedIndex].BackColor = Color.Yellow;

答案 3 :(得分:1)

  

Dim previousSelected As Integer

alert

答案 4 :(得分:0)

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
         previousSelected = ViewState("previousSelected")
        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
         ViewState("previousSelected") = index
    End If
End Sub