0

我有一个 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
4

3 回答 3

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
于 2013-07-17T14:26:09.550 回答
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

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

于 2013-07-19T17:23:45.847 回答
-1

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

否则页面将在选择和重新选择时重新加载。

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

于 2013-07-17T14:21:29.333 回答