.Net, ASP.NET, GridView, Programming, VB.NET, Web Development, WebForms

ASP.NET GridView RowUpdating Event Handler – Programmatically Reference Controls and set SqlDataSource UpdateParameters


Visual Studio allows developers to quickly put together a WebForms page simply using drag and drop options from the Visual Studio editor. Binding form controls is mostly a simple matter of attaching a DataSource and setting your controls to reference the correct field in the DataSource.

Binding a GridView control can sometimes get tricky when using the VisualStudio editor, especially when working with DropDownList controls within the grid.

Today I was working on getting a row of the GridView control to save the contents of a DropDownList, and needed to add some custom code to set values passed to the SqlDataSource control on the update event.


I found that simple data binding of my DropDownList in my GridView to my SqlDataSource was not going to be enough. I would need to set values passed to the SqlDataSource control on the update event, and as part of this I would need to reference the controls contained in the row being updated.

To do this, I created an instance of the RowUpdating event for the GridView. This event passes the necessary information to the method via the GridViewUpdateEventArgs parameter.

Then within the new method one can access controls within the GridView row being edited by referencing the RowIndex attribute of the GridView’s Rows collection.

So for example to reference a DropDownList within the GridView Row, use the syntax: CType(GVSupportInfo.Rows(e.RowIndex).FindControl(“ddlListName”), DropDownList)

Likewise to reference a Label within the GridView Row, use the syntax: CType(GVSupportInfo.Rows(e.RowIndex).FindControl(“lblLabelName”), Label)

Basically this is quite straightforward once one knows the syntax for referencing a control within a GridView Row.

To finish up then one has to set the SqlDataSource UpdateParameters to the desired values. The syntax for doing so is: SqlDSupportInfo.UpdateParameters(“Support_Level”).DefaultValue =

Here is example code for a complete GridView RowUpdating event handler that retrieves the values of a DropDownList and a Label for a specific row being edited within the GridView’s Rows collection:

 Protected Sub GVSupportInfo_RowUpdating(sender As Object, e As System.Web.UI.WebControls.GridViewUpdateEventArgs)
 Dim lblServiceLevel As DropDownList = CType(GVSupportInfo.Rows(e.RowIndex).FindControl("ddlServiceLevelList"), DropDownList)
 Dim lblSupportDate As Label = CType(GVSupportInfo.Rows(e.RowIndex).FindControl("Label20e"), Label)

 SqlDSupportInfo.UpdateParameters("Support_Level").DefaultValue = lblServiceLevel.SelectedItem.Value
 SqlDSupportInfo.UpdateParameters("Support_Delivered_Date").DefaultValue = lblSupportDate.Text
 End Sub



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s