• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4590
  • Last Modified:

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?


  • 4
  • 3
1 Solution
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)
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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...
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!

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now