Change The Font Color Gridview Row Based On A Columns Value. Cant Index Gridviewrow?
i am getting a syntax error at drr(5) which 5 is the column i want to base the color change on. this method works when i am using a dataset Dim Land As String = 'Land'
Solution 1:
Dim a AsString = GridView1.Rows(0).Cells(0).Textuse Text variable , that is the cell text word.
Solution 2:
When you want to access a cell in a grid view row, you should use the cell property of the row. In your example you need to write drr.Cells(5).ToString() As in
Dim Land AsString = "Land"Dim Air AsString = "Air"Dim Cruise AsString = "Cruise"Dim y AsStringForEach 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
EndIfNextAlso I find that it's better to give the row a class and then change the color using css.
Solution 3:
You should apply a CssClass according to this value:
for example:
Protected Sub Page_Load(ByVal sender AsObject, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
BindGrid()
End If
End Sub
Private Sub BindGrid()
Dim dt AsNew 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 AsObject, 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
The important part is in RowDataBound that is called automatically if you bind the GridView. If you don't want to name the CssClass exactly like the text that is displayed, you could use If...Else or Select Case to set the CSS-Class.
Solution 4:
And about changing the TableCells? This doen't work too?
gridview.Rows[0].Cells[0].ForeColor = ColorTranslator.FromHtml("#0000FF");
In C#, but give a try.
Solution 5:
ProtectedSub grdUsers_RowDataBound(ByVal sender AsObject, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdUsers.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow ThenForEach row As TableCell In e.Row.Cells
If e.Row.Cells(11).Text = "Approved"ThenFor i AsInteger = 0To11
e.Row.Cells(i).ForeColor = System.Drawing.Color.Green
NextEndIfIf e.Row.Cells(11).Text = "Rejected"ThenFor i AsInteger = 0To11
e.Row.Cells(i).ForeColor = System.Drawing.Color.Red
NextEndIfNextEndIfEndSub
Post a Comment for "Change The Font Color Gridview Row Based On A Columns Value. Cant Index Gridviewrow?"