在gridview RowCommand()中更改所选单元格的颜色

时间:2013-07-17 14:08:54

标签: html asp.net vb.net visual-studio-2010 visual-studio-2012

我有一个10行3列的Gridview。我想在单击时更改单元格的颜色。然后,如果用户再次单击它,请删除颜色。

通过此代码,我已将行号存储在index

Protected Sub GridView6_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView6.RowCommand
        Dim index As Integer = Convert.ToInt32(e.CommandArgument)
                    // Here I want to change my selected cell color

3 个答案:

答案 0 :(得分:0)

应该工作

Dim index As Integer = Convert.ToInt32(e.CommandArgument)
If (GridView1.Rows(index).BackColor = Drawing.Color.Aqua) Then
    GridView1.Rows(index).BackColor = Drawing.Color.White
Else
    GridView1.Rows(index).BackColor = Drawing.Color.Aqua
End If

答案 1 :(得分:0)

最后我找到了一个技巧。我不确定这是否是正确的方式,但是它对我有用。

Protected Sub GridView6_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView6.RowCommand
    Dim index As Integer = Convert.ToInt32(e.CommandArgument)
    Dim r As GridViewRow = GridView6.Rows(index)
    Dim cell1 As Button = r.Cells(0).Controls(0)
    Dim c1 As String = RTrim(cell1.Text)
    Dim cell2 As Button = r.Cells(1).Controls(0)
    Dim c2 As String = RTrim(cell2.Text)
    Dim cell3 As Button = r.Cells(2).Controls(0)
    Dim c3 As String = RTrim(cell3.Text)

    Dim cellname As String = RTrim(e.CommandName)

     If c1 = cellname Then
        If cell1.BackColor = Drawing.Color.OliveDrab Then
            cell1.BackColor = Drawing.Color.Linen

        Else
            cell1.BackColor = Drawing.Color.OliveDrab

        End If
    End If
    // AND same for C2 and C3

它既可以着色也可以变色;)

答案 2 :(得分:-1)

如果这是一个网页(因为你标记了asp.net),我会用javascript进行客户端支持。

否则页面将重新加载选择并重新选择。

将单元格添加到类中,并向其中添加单击事件。 更改css背景或将它们添加到新类。