2

是否有在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

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

有没有办法一次只突出显示一行(当前选定的行)?

谢谢

4

5 回答 5

3

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

于 2012-11-26T15:59:54.310 回答
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
于 2012-11-26T15:42:11.263 回答
2

I do it in the SelectedIndexChanged event and it works for me.

GridView1.Rows[GridView1.SelectedIndex].BackColor = Color.Yellow;
于 2013-03-04T10:00:14.533 回答
1

Dim previousSelected As Integer

        If e.CommandName = "Select" Then
            previousSelected = GetVal(ViewState("previousSelected"))
            Dim index As Integer = Convert.ToInt32(e.CommandArgument)
            Dim selectedRow As GridViewRow = grvOptionset.Rows(previousSelected)
            selectedRow.Style.Add("background-color", "#ffffff") 'change it back to original color
            selectedRow = grvOptionset.Rows(index)
            selectedRow.Style.Add("background-color", "#ffcccc") 'change the color of the new row
            ViewState("previousSelected") = index
        End If
于 2015-12-24T04:52:17.937 回答
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
于 2015-12-24T04:48:27.530 回答