GridView RowCreated Event Work With Cell Value

I would like to conditionally add a command link in the rightmost column of a gridview based upon the value in col(1) of a gridview during the RowCreated Event.  Code sample follows --

Protected Sub grdAppointments_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdAppointments.RowCreated

       If (e.Row.RowType = DataControlRowType.DataRow) Then
            If CType(e.Row.Cells(1).Text, Date) > Date.Today Then
                Dim l1 As New LinkButton()
                l1.ID = "Cancel"
                l1.Text = "Cancel Appt"
                l1.CommandName = "Update"
                l1.Visible = True
                l1.Attributes.Add("onClick", "return confirm('Cancel?');")

                Dim tc As New TableCell()
                e.Row.Cells.AddAt(intCol, tc)

            End If
       End if
End Sub

How do I access the value of e.Row.Cells(1) in the GridView_RowCreated event so I could dynamically add the linkbutton?


Who is Participating?
David RobitailleConnect With a Mentor Analyst ProgrammerCommented:
well. I usually don`t "create" control, I prefer to play with the Visible property to do so...
David RobitailleAnalyst ProgrammerCommented:
  1. trhasform the colum into a template field
  2. Use e.Row.findcontrol to find the control you put in the template field.
albertpetrmichlAuthor Commented:
Are you telling me it's not possible to obtain the cell(1) value from an ASP:boundfield column in the RowCreated event?

I've been doing some more research and came on this sequence --

Dim var1 As DataBoundLiteralControl = CType(e.Row.Cells(1).Controls(0), DataBoundLiteralControl)
response.write (var1.Text)

This is not erroring out however does not come up with the value of column 1 which should be a date.

Any suggestions?

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

David RobitailleAnalyst ProgrammerCommented:
Never had any succes getting control value from a ASP:boundfield so i alway turned them into template field. but it may be possible.
But i think you problem is somewere. you are useing the RowCreated event. the problem is the control value is not set in that event. you should use the RowDataBound event.
you could also access the data directly in that event using the e.Row.DataItem.
you may have to cast it to get the datatype:
CType(CType(e.Row.DataItem, System.Data.DataRowView).Row, stronglyTypedDataType)
albertpetrmichlAuthor Commented:
Good suggestion.     If I change to the RowDataBound event to grab the value can I add a column at that point, or must this be done in the RowCreated Event?

If the dynamically added link control must be added in the RowCreated event than I guess I'm stuck with the the Template field suggestion.

albertpetrmichlAuthor Commented:
Appreciate your quick response on this.  You've given me several ways to attack this problem.
David RobitailleAnalyst ProgrammerCommented:
Glad i could help you!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.