Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > conditional formatting of text in a gridview

Reply
Thread Tools

conditional formatting of text in a gridview

 
 
Plateriot
Guest
Posts: n/a
 
      12-08-2008
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?
 
Reply With Quote
 
 
 
 
Joern Schou-Rode
Guest
Posts: n/a
 
      12-08-2008
On Mon, 08 Dec 2008 19:11:01 +0100, Plateriot
<(E-Mail Removed)> wrote:

> 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

--
Joern Schou-Rode
http://malamute.dk/
 
Reply With Quote
 
 
 
 
Plateriot
Guest
Posts: n/a
 
      12-08-2008
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.




"Joern Schou-Rode" wrote:

> On Mon, 08 Dec 2008 19:11:01 +0100, Plateriot
> <(E-Mail Removed)> wrote:
>
> > 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
>
> --
> Joern Schou-Rode
> http://malamute.dk/
>

 
Reply With Quote
 
Paul Shapiro
Guest
Posts: n/a
 
      12-09-2008
In the RowDataBound event handler:
DataRowView rowView = (DataRowView)e.Row.DataItem;
if (rowView != null)
{
//Retrieve data value
datatype localVariable = (datatype)rowView["fieldName"];
}

"Plateriot" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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.
>
> "Joern Schou-Rode" wrote:
>
>> On Mon, 08 Dec 2008 19:11:01 +0100, Plateriot
>> <(E-Mail Removed)> wrote:
>>
>> > 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
>> --
>> Joern Schou-Rode
>> http://malamute.dk/


 
Reply With Quote
 
Joern Schou-Rode
Guest
Posts: n/a
 
      12-09-2008
On Tue, 09 Dec 2008 01:28:02 +0100, Paul Shapiro
<(E-Mail Removed)> wrote:
> 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

--
Joern Schou-Rode
http://malamute.dk/
 
Reply With Quote
 
Plateriot
Guest
Posts: n/a
 
      12-10-2008

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?

"Joern Schou-Rode" wrote:

> On Tue, 09 Dec 2008 01:28:02 +0100, Paul Shapiro
> <(E-Mail Removed)> wrote:
> > 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
>
> --
> Joern Schou-Rode
> http://malamute.dk/
>

 
Reply With Quote
 
Plateriot
Guest
Posts: n/a
 
      12-10-2008
Ignore my last request - You had the answer.
Thanks.

"Joern Schou-Rode" wrote:

> On Tue, 09 Dec 2008 01:28:02 +0100, Paul Shapiro
> <(E-Mail Removed)> wrote:
> > 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
>
> --
> Joern Schou-Rode
> http://malamute.dk/
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Conditional formatting of nested gridview cells Nitz ASP .Net 1 04-06-2009 01:15 PM
Gridview Conditional Formatting ? euan@madprops.org ASP .Net 2 10-29-2007 05:39 PM
Gridview - conditional formatting Not Me ASP .Net 9 11-08-2006 04:33 PM
? ELSE Conditional Comment / Using Conditional Comments Inside Other Tags To Comment Out Attributes Alec S. HTML 10 04-16-2005 02:21 AM
data grid conditional row formatting Brian Henry ASP .Net 2 07-08-2004 04:10 PM



Advertisments