GridView DataRowBound question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pvong

    GridView DataRowBound question

    Doing this in VB.NET with VS2008.

    I have a simple GridView with two columns. Column1 Header is called
    "Reason" and the rows contain a Label called "ReasonL". Column2 header is
    called Add/Edit and the row has a LinkButton called "AddEditLB" . Basically,
    it looks like this.

    Reason Add/Edit
    ReasonL AddEditLB

    All I want to do is make AddEditLB Text to say "Add" if ReasonL is
    string.empty and "Edit" if it's not. This is what I tried in the
    RowDataBound event and it didn't work:

    Protected Sub GridView1_RowDa taBound(ByVal sender As Object, ByVal e As
    System.Web.UI.W ebControls.Grid ViewRowEventArg s) Handles
    GridView1.RowDa taBound

    If e.Row.RowType = DataControlRowT ype.DataRow Then
    If e.Row.DataItem( "ReasonL").ToSt ring = String.Empty Then
    e.Row.DataItem( "AddEditLB" ) = "Add"
    Else
    e.Row.DataItem( "AddEditLB" ) = "Edit"
    End If
    End If

    End Sub

    Can someone tell me what I did wrong?
    This is the error msg I got.

    ReasonL is neither a DataColumn nor a DataRelation for table DefaultView.

    Thanks!


  • Mark Rae [MVP]

    #2
    Re: GridView DataRowBound question

    "pvong" <phillip*at*yah oo*dot*comwrote in message
    news:eJJXzHuAJH A.2276@TK2MSFTN GP04.phx.gbl...
    e.Row.DataItem( "AddEditLB" ) = "Add"
    What happens if you try:

    e.Row.Cells(2). Text = "Add"


    --
    Mark Rae
    ASP.NET MVP


    Comment

    • pvong

      #3
      Re: GridView DataRowBound question

      Mark - This does work, but it only puts the word "Add" or "Edit" in my
      column where I had my AddEdit LinkButton. It's just text and no LinkButton.
      I want to assign the text for the LB in each row. My users need to have
      access to the LB.


      "Mark Rae [MVP]" <mark@markNOSPA Mrae.netwrote in message
      news:%23WUJcSuA JHA.4964@TK2MSF TNGP02.phx.gbl. ..
      "pvong" <phillip*at*yah oo*dot*comwrote in message
      news:eJJXzHuAJH A.2276@TK2MSFTN GP04.phx.gbl...
      >
      >e.Row.DataItem ("AddEditLB" ) = "Add"
      >
      What happens if you try:
      >
      e.Row.Cells(2). Text = "Add"
      >
      >
      --
      Mark Rae
      ASP.NET MVP
      http://www.markrae.net

      Comment

      • Mark Rae [MVP]

        #4
        Re: GridView DataRowBound question

        "pvong" <phillip*at*yah oo*dot*comwrote in message
        news:O8IENBvAJH A.3728@TK2MSFTN GP03.phx.gbl...

        [top-posting corrected]
        >>e.Row.DataIte m("AddEditLB" ) = "Add"
        >>
        >What happens if you try:
        >>
        >e.Row.Cells(2) .Text = "Add"
        >
        This does work, but it only puts the word "Add" or "Edit" in my column
        where I had my AddEdit LinkButton. It's just text and no LinkButton. I
        want to assign the text for the LB in each row. My users need to have
        access to the LB.
        OK, then. How about:

        DirectCast(e.Ro w.Cells(2).Find Control("AddEdi tLB"), Button).Text = "Add"

        The syntax may not be 100% accurate, as I never go anywhere near VB.NET, but
        should be close enough...


        --
        Mark Rae
        ASP.NET MVP


        Comment

        • pvong

          #5
          Re: GridView DataRowBound question

          This worked perfectly. Now I need to do some reading on what DirectCast
          actually does. Learned something new today. Thanks!

          "Mark Rae [MVP]" <mark@markNOSPA Mrae.netwrote in message
          news:%230%23sRU vAJHA.5316@TK2M SFTNGP04.phx.gb l...
          "pvong" <phillip*at*yah oo*dot*comwrote in message
          news:O8IENBvAJH A.3728@TK2MSFTN GP03.phx.gbl...
          >
          [top-posting corrected]
          >
          >>>e.Row.DataIt em("AddEditLB" ) = "Add"
          >>>
          >>What happens if you try:
          >>>
          >>e.Row.Cells(2 ).Text = "Add"
          >>
          >This does work, but it only puts the word "Add" or "Edit" in my column
          >where I had my AddEdit LinkButton. It's just text and no LinkButton. I
          >want to assign the text for the LB in each row. My users need to have
          >access to the LB.
          >
          OK, then. How about:
          >
          DirectCast(e.Ro w.Cells(2).Find Control("AddEdi tLB"), Button).Text = "Add"
          >
          The syntax may not be 100% accurate, as I never go anywhere near VB.NET,
          but should be close enough...
          >
          >
          --
          Mark Rae
          ASP.NET MVP
          http://www.markrae.net

          Comment

          Working...