2011-02-01 75 views
0

我在DRR(5)得到一個語法錯誤,其中5是我想基於顏色更改的列。 當我使用的數據集根據列值更改字體顏色gridview行。不能索引gridviewrow?

Dim Land As String = "Land" 
     Dim Air As String = "Air" 
     Dim Cruise As String = "Cruise" 

     Dim y As String 

     For Each drr As gridviewrow In GridView2.Rows 
      y = drr(5).ToString() 
      If y = Land Then 
       e.Row.ForeColor = System.Drawing.Color.LightGreen 
      ElseIf y = Air Then 
       e.Row.ForeColor = System.Drawing.Color.Red 
      ElseIf y = Cruise Then 
       e.Row.ForeColor = System.Drawing.Color.Green 
      End If 


     Next 

回答

0

當你想在一個網格視圖行可看到細胞這種方法可行,你應該使用該行的單元格屬性。在您的例子中,你需要寫drr.Cells(5)的ToString() 作爲

Dim Land As String = "Land" 
    Dim Air As String = "Air" 
    Dim Cruise As String = "Cruise" 

    Dim y As String 

    For Each drr As gridviewrow In GridView2.Rows 
     y = drr.Cells(5).ToString() 
     If y = Land Then 
      e.Row.ForeColor = System.Drawing.Color.LightGreen 
     ElseIf y = Air Then 
      e.Row.ForeColor = System.Drawing.Color.Red 
     ElseIf y = Cruise Then 
      e.Row.ForeColor = System.Drawing.Color.Green 
     End If 


    Next 

此外,我覺得這是更好地給該行一類,然後使用CSS改變顏色。

+0

奇怪沒有語法錯誤,但顏色仍呈現爲黑色 – MyHeadHurts 2011-02-01 20:38:39

0

你應該根據這個值應用的CssClass:

例如:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    If Not IsPostBack Then 
     BindGrid() 
    End If 
End Sub 

Private Sub BindGrid() 
    Dim dt As New DataTable 
    dt.Columns.Add("ID", GetType(Int32)).AutoIncrement = True 
    dt.Columns.Add("Style", GetType(String)) 
    dt.PrimaryKey = New DataColumn() {dt.Columns("ID")} 
    Dim newRow As DataRow = dt.NewRow 
    newRow("Style") = "Land" 
    dt.Rows.Add(newRow) 
    newRow = dt.NewRow 
    newRow("Style") = "Air" 
    dt.Rows.Add(newRow) 
    newRow = dt.NewRow 
    newRow("Style") = "Cruise" 
    dt.Rows.Add(newRow) 
    Me.GridView1.DataSource = dt 
    Me.GridView1.DataBind() 
End Sub  

Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound 
    If e.Row.RowType = DataControlRowType.DataRow Then 
     Dim dr As DataRow = DirectCast(e.Row.DataItem, DataRowView).Row 
     e.Row.CssClass = DirectCast(dr("Style"), String) 
    End If 
End Sub 

的重要組成部分,是的RowDataBound被稱爲如果自動綁定GridView的。如果您不想將CssClass命名爲與顯示的文本完全相同,則可以使用If...ElseSelect Case來設置CSS-Class。

+0

問題是我使用將SqlDataSource,我不是手動編碼的連接和SQL,即時沒有創建數據表 – MyHeadHurts 2011-02-01 21:31:26

+0

Nevertheles`RowDataBound`將隱含地稱呼。看看下面的MSDN鏈接進一步的信息:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx – 2011-02-01 21:35:13

0

而關於改變TableCells?這也不行?

gridview.Rows[0].Cells[0].ForeColor = ColorTranslator.FromHtml("#0000FF"); 

在C#中,但試一試。

1
Dim a As String = GridView1.Rows(0).Cells(0).Text 

使用Text變量,即單元格文字。

0
Protected Sub grdUsers_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdUsers.RowDataBound 
If e.Row.RowType = DataControlRowType.DataRow Then 
     For Each row As TableCell In e.Row.Cells 
      If e.Row.Cells(11).Text = "Approved" Then 
       For i As Integer = 0 To 11 
        e.Row.Cells(i).ForeColor = System.Drawing.Color.Green 
       Next 
      End If 
      If e.Row.Cells(11).Text = "Rejected" Then 
       For i As Integer = 0 To 11 
        e.Row.Cells(i).ForeColor = System.Drawing.Color.Red 
       Next 
      End If 
     Next 
    End If 
End Sub