conditional formatting of text in a gridview

P

Plateriot

I was looking to format the text of certain values in my gridview, but I have
a question first:

1) If I don't use an ObjectDataSource - but instead manually populate the
gridview - does it invalidate the RowDataBound event?

The reason I ask - is that even though I can see the values in this
gridview, it seems as though I am asking for them too early

Here's my RowDataBound event :

Protected Sub gvBP_Detail_AM_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs)

If e.Row.RowType = DataControlRowType.DataRow Then

Dim strSBP As String = e.Row.Cells(0).Text
Dim strDBP As String = e.Row.Cells(1).Text


Dim iSBP, iDBP, iSBPTarget, iDBPTarget As Integer


iSBP = Int32.Parse(strSBP)
iDBP = Int32.Parse(strDBP)
iSBPTarget = Int32.Parse(Me.txtSBP_Target.Text)
iDBPTarget = Int32.Parse(Me.txtDBP_Target.Text)


If iSBP > iSBPTarget Then e.Row.Cells(0).ForeColor =
Drawing.Color.Red
If iDBP > iDBPTarget Then e.Row.Cells(1).ForeColor =
Drawing.Color.Red

End If

End Sub

but, right off the bat, it doesn't see any value in e.row.cells(0).text -
which I can plainly see has the text "120" --- any idea why?
 
J

Joern Schou-Rode

but, right off the bat, it doesn't see any value in e.row.cells(0).text -
which I can plainly see has the text "120" --- any idea why?

Let me suggest an alternative approach: Instead of parsing the integer
back from its string representation in the grid, you should be able to
read the integer value directly from the data item within your
RowDataBound handler.

The e.DataItem holds a reference to whatever you the row has been bound
to, and by applying the right cast you should be able to fetch the value
directly from here.

I hope this helps :)
 
P

Plateriot

I found
e.Row.DataItem

but how do I reference the appropriate column...?
e.row.cells(0) grabs the string that I'm looking for.
 
P

Paul Shapiro

In the RowDataBound event handler:
DataRowView rowView = (DataRowView)e.Row.DataItem;
if (rowView != null)
{
//Retrieve data value
datatype localVariable = (datatype)rowView["fieldName"];
}
 
J

Joern Schou-Rode

DataRowView rowView = (DataRowView)e.Row.DataItem;

Since the OP was using VB.NET:

Dim rowView As DataRowView = CType(e.Row.DataItem,DataRowView)
If (Not rowView Is Nothing) Then
Dim localVariable As datatype = CType(rowView("fieldName"),datatype)
End If

If you are binding to some kind of business object:

Dim cust As Customer = CType(e.Row.DataItem,Customer)
If (Not cust Is Nothing) Then
iSBP = cust.MagicNumber
End If
 
P

Plateriot

I tried all of the above and I continue to get a null string or zero for the
cell that I am checking. Which made me wonder if I'm reading the value too
early.

If I manually bind my datasource, vs using an ObjectDataSource, does that
affect the RowDataBound event not to catch the value at the moment I am
expecting to find it?

I tried it by binding to an ObjectDataSource and it picked up the value - so
here's my question:

Is there a different event to read the value in the gridview cell or item
other than the RowDataBound event --? or is there a way to delay, when it
looks for the value to make sure it catches it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top