conditional formatting of text in a gridview

Discussion in 'ASP .Net' started by Plateriot, Dec 8, 2008.

  1. Plateriot

    Plateriot Guest

    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?
     
    Plateriot, Dec 8, 2008
    #1
    1. Advertising

  2. On Mon, 08 Dec 2008 19:11:01 +0100, Plateriot
    <> 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/
     
    Joern Schou-Rode, Dec 8, 2008
    #2
    1. Advertising

  3. Plateriot

    Plateriot Guest

    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
    > <> 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/
    >
     
    Plateriot, Dec 8, 2008
    #3
  4. Plateriot

    Paul Shapiro Guest

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

    "Plateriot" <> wrote in message
    news:...
    >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
    >> <> 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/
     
    Paul Shapiro, Dec 9, 2008
    #4
  5. On Tue, 09 Dec 2008 01:28:02 +0100, Paul Shapiro
    <> 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/
     
    Joern Schou-Rode, Dec 9, 2008
    #5
  6. Plateriot

    Plateriot Guest

    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
    > <> 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/
    >
     
    Plateriot, Dec 10, 2008
    #6
  7. Plateriot

    Plateriot Guest

    Ignore my last request - You had the answer.
    Thanks.

    "Joern Schou-Rode" wrote:

    > On Tue, 09 Dec 2008 01:28:02 +0100, Paul Shapiro
    > <> 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/
    >
     
    Plateriot, Dec 10, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Henry

    data grid conditional row formatting

    Brian Henry, Jul 8, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    2,088
    Brian Henry
    Jul 8, 2004
  2. Replies:
    2
    Views:
    10,426
    FatherJ
    Oct 29, 2007
  3. Not Me

    Gridview - conditional formatting

    Not Me, Feb 9, 2006, in forum: ASP .Net
    Replies:
    9
    Views:
    6,818
    code_monkey
    Nov 8, 2006
  4. Alec S.
    Replies:
    10
    Views:
    10,170
    Alec S.
    Apr 16, 2005
  5. Nitz
    Replies:
    1
    Views:
    1,016
    Sam Evans
    Apr 6, 2009
Loading...

Share This Page