Gridview control with dropdownlist and DBNull

With Templatefields it is easy to use a dropdown control for value selection. The problem begins if the field in the table have a value which is not in the list of the dropdown. Databinding fails on runtime.

SelectedValue='<%# Bind("PostalCode") %>'

Especially field which contains null values making problems. You need to write code.

1) Remove the Databinding

2) Fill the value in the rowcreatedevent. The following code is searching for the value in the list and select it.

Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)

If (e.Row.RowState And DataControlRowState.Edit) <> 0 OrElse (e.Row.RowState And DataControlRowState.Insert) <> 0 Then


Dim tmp As DropDownList = CType(e.Row.FindControl("drpPLZ"), DropDownList)


If Not IsNothing(tmp.Items.FindByValue(DataBinder.Eval(e.Row.DataItem, "postalcode").ToString())) Then

tmp.SelectedValue = DataBinder.Eval(e.Row.DataItem, "postalcode").ToString()

End If

Catch ex As Exception

End Try

End If

End Sub

Casue you are in the created event the binding of dropdown must be done. You do not neet that if you have a static list of entrys in the dropdownlíst

A part of the HTML of the Gridview

<asp:TemplateField HeaderText="PostalCode" SortExpression="PostalCode">


<asp:SqlDataSource ID="sqlDSPLZ" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString1 %>"

SelectCommand="SELECT DISTINCT PostalCode FROM Customers ORDER BY PostalCode" CacheDuration="600" EnableCaching=true></asp:SqlDataSource>

<asp:DropDownList ID="drpPLZ" runat="server"

DataSourceID="sqlDSPLZ" DataValueField="postalcode" DataTextField="postalcode" >




<asp:Label ID="Label1" runat="server" Text='<%# Bind("PostalCode") %>'></asp:Label>



The data should be load only once from DB so enable caching or put the sqldatasource outside of the gridview.

3) Save the data

You must hook into the saving process with the Rowupdating event. The  parameter NewValues must be set with the SelectedValue from DropDownlist.

Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs)

e.NewValues("postalcode") = CType(GridView1.Rows(e.RowIndex).FindControl("drpPLZ"), DropDownList).SelectedValue

End Sub

This sample is tooken from the german community website



  • Thank you it's work perfectly

  • Hi,
    I am running similar code where I bind the dropdownlist in the edititemtemplate from the codebehind as follows:

    Protected Sub GridViewRowsCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowDataBound
    If Me.GridView1.EditIndex > -1 Then
    If e.Row.RowType = DataControlRowType.DataRow Then
    If Not IsNothing(e.Row.FindControl("ddl_ClinicNames")) Then

    Dim ds_Clinics As DataSet = New DataSet()
    ds_Clinics = Me.m_objUtil.Clinic_ListAll("1")
    Dim ddl As DropDownList
    ddl = CType(e.Row.FindControl("ddl_ClinicNames"), DropDownList)
    'ddl.DataSource = CType(ds_Clinics, DataSet).Tables(0)

    ddl.DataSource = ds_Clinics.Tables(0)
    ddl.DataTextField = "ClinicName"
    ddl.DataValueField = "UID"

    ddl.SelectedValue = Me.m_objUtil.SubCompetency_GetClinicID(e.Row.Cells(1).Text)
    End If
    End If
    End If
    End Sub

    Then I attempt to retrieve the value when the user updates the row, but it always takes the value that was initially selected. I suspect this is because the binding and selected value function above are running before the rowupdating. The rowupdating is below (I took out some code to make it shorter):

    Protected Sub GridUpdateCommand(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) Handles GridView1.RowUpdating

    Dim str_ClinicID As String = String.Empty
    If Not IsNothing(Me.GridView1.Rows(e.RowIndex).FindControl("ddl_ClinicNames")) Then

    str_ClinicID = CType(Me.GridView1.Rows(e.RowIndex).FindControl("ddl_ClinicNames"), DropDownList).SelectedValue
    End If

    Me.m_objUtil.SubCompetency_Update(uid, compid, e.NewValues(0), str_ClinicID, e.NewValues(1), e.NewValues(2))
    Me.GridView1.EditIndex = -1
    End Sub


Comments have been disabled for this content.