GridView RowCreated Event Work With Cell Value

Posted on 2008-11-10
Last Modified: 2012-05-05
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?


Question by:albertpetrmichl
    LVL 18

    Expert Comment

    1. trhasform the colum into a template field
    2. Use e.Row.findcontrol to find the control you put in the template field.

    Author Comment

    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?

    LVL 18

    Expert Comment

    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)

    Author Comment

    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.

    LVL 18

    Accepted Solution

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

    Author Closing Comment

    Appreciate your quick response on this.  You've given me several ways to attack this problem.
    LVL 18

    Expert Comment

    Glad i could help you!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now