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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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?

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)
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

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.

David RobitailleAnalyst ProgrammerCommented:
well. I usually don`t "create" control, I prefer to play with the Visible property to do so...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.