2013-07-17 46 views
0

我有一個10行和3列的GridView。點擊時我想改變單元格的顏色。然後,如果用戶再次單擊它,請刪除該顏色。更改GridView中選定單元格的顏色_RowCommand()

通過這個代碼我已經保存在我的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 
+0

對不起,我沒有得到U,我只是創建一個rowcommand事件,所以我可以做一些其他的東西太喜歡「如果e.CommandName =‘全部’然後... –

回答

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 

它的工作方式都對顏色和變色;)

-1

如果這是一個網頁(因爲您標記asp.net)我會做客戶端片面,與JavaScript。

否則頁面將在選後裝載並重新選擇。

將單元格添加到類中,並向它們添加單擊事件。 要麼改變CSS背景或將它們添加到新的類。

+0

錯了,asp.net不能重新加載頁面,它使用Javascript ... – Alker

+0

我不知道爲什麼,但我不能把點擊事件,也許是因爲它的databound gridview。 –

+0

你能夠把表單元格放在一個class? – Naoe

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 
+0

感謝您的回答,但它會改變選定的行顏色,我只想選定單元格顏色 –