vb.net gridview 突出显示当前行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13567859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:18:52  来源:igfitidea点击:

gridview highlighting current row

asp.net.netvb.net

提问by SkyeBoniwell

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:

我的 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

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

谢谢

回答by jonhopkins

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

回答by Clarice Bouwer

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

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

回答by Santosh Wavare

I do it in the SelectedIndexChangedevent and it works for me.

我在SelectedIndexChanged活动中这样做,它对我有用。

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

回答by Tapan

Dim previousSelected As Integer

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

回答by Tapan

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