2012-11-26 150 views
2

是否有內置方法突出顯示GridView中當前選定的行?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 

這凸顯了該行,但是一旦用戶按下另一行的按鈕,它仍然保留了所有的顏色先前按下的行。

是否有辦法讓每次只有一行(當前選定的行)被突出顯示?

感謝

回答

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 
2

我在SelectedIndexChanged事件中這樣做,它適用於我。

GridView1.Rows[GridView1.SelectedIndex].BackColor = Color.Yellow; 
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 
1

昏暗previousSelected作爲整數

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

好了,現在可以刪除一個答案,並添加一些特殊塔潘語言質量 – Drew

+0

如何寫像網與這種類型的代碼清潔。請告訴我 – Tapan

+0

突出顯示該塊,並擊中扭曲{}的東西。 – Drew