Since I'm not sure what the protocol is for posting an update to an original post, which was near resolution but the thread seemed to die. Apologies if there is a better way to revive that thread.
Anyway, I have found many ways to format cells within in the grid inclduing centering, formatting strings etc BUT i still cannot change the font color of the cell text based on a formula evaluation. Everything i try shows the cell as empty even though its
clear as day full of values on the webpage. The code below is for my databound event
Protected Sub gvDataRetrieval_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gvDataRetrieval.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
For Each cell As TableCell In e.Row.Cells
cell.Font.Size = 10
cell.HorizontalAlign = HorizontalAlign.Center
Next
e.Row.Cells(0).Text = Split(e.Row.Cells(0).Text, " ")(0)
For i As Integer = 1 To 2
e.Row.Cells(i).Text = FormatCurrency(e.Row.Cells(i).Text, 2).ToString()
'If Interger.Parse(e.Row.Cells(i).Text)< 0 Then e.Row.Cells(i).ForeColor = Drawing.Color.Red
Next
End If
End Sub
I also tried using the code below inside the For i as integer code above;
Dim value As Integer = CInt(DataBinder.Eval(e.Row.DataItem, e.Row.Cells(i).Text))
If value < 0 Then e.Row.Cells(i).ForeColor = Drawing.Color.Red
I'm sure there is a simple solution but im not seeing it
Forget the comment above, the code below does what I want. It colors the negative values in the grid with red font, the question is why does this work? How does the variable cellval possibly reference the actual cell i am setting the fontcolor to in the nect code line as it runs through the loop? Instead of hardcoding the column index numbers how can i retrieve the columns either via header name or getting the index number of the column from the headername
Protected Sub gvDataRetrieval_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gvDataRetrieval.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
For Each cell As TableCell In e.Row.Cells
cell.Font.Size = 10
cell.HorizontalAlign = HorizontalAlign.Center
Next
e.Row.Cells(0).Text = Split(e.Row.Cells(0).Text, " ")(0)
Dim cellval As Double = 0.0
For i As Integer = 1 To 2
e.Row.Cells(i).Text = FormatCurrency(e.Row.Cells(i).Text, 0).ToString()
cellval = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "pnl"))
If cellval < 0 Then e.Row.Cells(i).ForeColor = Drawing.Color.Red
Next
End If
End Sub
e.Row.Cells(i).BackColor = System.Drawing.Color.FromName("#149414")
e.Row.Cells(i).ForeColor = System.Drawing.Color.FromName("#149414") // Change HTML Color Code Accroding to your requirement!
Sincerely,
Mahad Bin Mukhtar Remember to Mark the replies as Answers
The easiest day was 'yesterday'.
MCP, MCSD
For .NET TECH Blog
Your missing the point, the code IS CHANGING the color, the question is why? The code doesnt read right, why does the
If cellval....... line change the fontcolor? cellval in no way , shape or form is referencing e.Row.Cells(i) so why is it evaluating and changing the fontcolor? and its doing it properly for both columns, one of which the header is "pnl" the other is "cumpnl" so why is just "pnl" in the DataBinder.Eval working for both? does the "pnl" paramter act like an Instring function?
For i As Integer = 1 To 2
e.Row.Cells(i).Text = FormatCurrency(e.Row.Cells(i).Text, 0).ToString()
cellval = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "pnl"))
If cellval < 0 Then e.Row.Cells(i).ForeColor = Drawing.Color.Red
Next
Let's make things simpler. How can I retrieve the column index numbers by iterating through a gridview and referencing the column header names, for example if i had a four column grid where the headers were date, dailypnl, cumpnlforweek, cumpnlformonth.
How can I get the column index numbers by header name? All these grids come in dynamically so I have to get their index number programatically since ill be supplied with the header name.
Well here is a solution, even though id still rather find the index numbers by column names
Protected Sub gvDataRetrieval_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gvDataRetrieval.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
For i As Integer = 0 To e.Row.Cells.Count - 1
e.Row.Cells(i).Font.Size = 10
e.Row.Cells(i).HorizontalAlign = HorizontalAlign.Center
If i > 0 Then
If Convert.ToDouble(e.Row.Cells(i).Text) < 0 Then e.Row.Cells(i).ForeColor = Drawing.Color.Red
End If
Next
e.Row.Cells(0).Text = Split(e.Row.Cells(0).Text, " ")(0)
End If
End Sub
Marked as answer by dinotom on Jun 03, 2012 11:00 AM
dinotom
Member
24 Points
69 Posts
changing gridview font color revisited
Jun 02, 2012 10:50 PM|LINK
Since I'm not sure what the protocol is for posting an update to an original post, which was near resolution but the thread seemed to die. Apologies if there is a better way to revive that thread.
Anyway, I have found many ways to format cells within in the grid inclduing centering, formatting strings etc BUT i still cannot change the font color of the cell text based on a formula evaluation. Everything i try shows the cell as empty even though its clear as day full of values on the webpage. The code below is for my databound event
Protected Sub gvDataRetrieval_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gvDataRetrieval.RowDataBound If e.Row.RowType = DataControlRowType.DataRow Then For Each cell As TableCell In e.Row.Cells cell.Font.Size = 10 cell.HorizontalAlign = HorizontalAlign.Center Next e.Row.Cells(0).Text = Split(e.Row.Cells(0).Text, " ")(0) For i As Integer = 1 To 2 e.Row.Cells(i).Text = FormatCurrency(e.Row.Cells(i).Text, 2).ToString() 'If Interger.Parse(e.Row.Cells(i).Text)< 0 Then e.Row.Cells(i).ForeColor = Drawing.Color.Red Next End If End SubI also tried using the code below inside the For i as integer code above;
I'm sure there is a simple solution but im not seeing it
Forget the comment above, the code below does what I want. It colors the negative values in the grid with red font, the question is why does this work? How does the variable cellval possibly reference the actual cell i am setting the fontcolor to in the nect code line as it runs through the loop? Instead of hardcoding the column index numbers how can i retrieve the columns either via header name or getting the index number of the column from the headername
Protected Sub gvDataRetrieval_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gvDataRetrieval.RowDataBound If e.Row.RowType = DataControlRowType.DataRow Then For Each cell As TableCell In e.Row.Cells cell.Font.Size = 10 cell.HorizontalAlign = HorizontalAlign.Center Next e.Row.Cells(0).Text = Split(e.Row.Cells(0).Text, " ")(0) Dim cellval As Double = 0.0 For i As Integer = 1 To 2 e.Row.Cells(i).Text = FormatCurrency(e.Row.Cells(i).Text, 0).ToString() cellval = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "pnl")) If cellval < 0 Then e.Row.Cells(i).ForeColor = Drawing.Color.Red Next End If End SubMahadTECH
Star
8976 Points
1659 Posts
Re: changing gridview font color revisited
Jun 03, 2012 12:23 AM|LINK
Try
e.Row.Cells(i).BackColor = System.Drawing.Color.FromName("#149414") e.Row.Cells(i).ForeColor = System.Drawing.Color.FromName("#149414") // Change HTML Color Code Accroding to your requirement!Mahad Bin Mukhtar
Remember to Mark the replies as Answers
The easiest day was 'yesterday'.
MCP, MCSD
For .NET TECH Blog
dinotom
Member
24 Points
69 Posts
Re: changing gridview font color revisited
Jun 03, 2012 02:18 AM|LINK
If cellval....... line change the fontcolor? cellval in no way , shape or form is referencing e.Row.Cells(i) so why is it evaluating and changing the fontcolor? and its doing it properly for both columns, one of which the header is "pnl" the other is "cumpnl" so why is just "pnl" in the DataBinder.Eval working for both? does the "pnl" paramter act like an Instring function?For i As Integer = 1 To 2 e.Row.Cells(i).Text = FormatCurrency(e.Row.Cells(i).Text, 0).ToString() cellval = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "pnl")) If cellval < 0 Then e.Row.Cells(i).ForeColor = Drawing.Color.Red Nextdinotom
Member
24 Points
69 Posts
Re: changing gridview font color revisited
Jun 03, 2012 02:29 AM|LINK
Let's make things simpler. How can I retrieve the column index numbers by iterating through a gridview and referencing the column header names, for example if i had a four column grid where the headers were date, dailypnl, cumpnlforweek, cumpnlformonth. How can I get the column index numbers by header name? All these grids come in dynamically so I have to get their index number programatically since ill be supplied with the header name.
dinotom
Member
24 Points
69 Posts
Re: changing gridview font color revisited
Jun 03, 2012 11:00 AM|LINK
Well here is a solution, even though id still rather find the index numbers by column names
Protected Sub gvDataRetrieval_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gvDataRetrieval.RowDataBound If e.Row.RowType = DataControlRowType.DataRow Then For i As Integer = 0 To e.Row.Cells.Count - 1 e.Row.Cells(i).Font.Size = 10 e.Row.Cells(i).HorizontalAlign = HorizontalAlign.Center If i > 0 Then If Convert.ToDouble(e.Row.Cells(i).Text) < 0 Then e.Row.Cells(i).ForeColor = Drawing.Color.Red End If Next e.Row.Cells(0).Text = Split(e.Row.Cells(0).Text, " ")(0) End If End Sub