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

Try

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

tmp.DataBind()

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">

<EditItemTemplate>

<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:DropDownList>

</EditItemTemplate>

<ItemTemplate>

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

</ItemTemplate>

</asp:TemplateField>

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 http://www.devtrain.de

 

Published Thursday, February 02, 2006 10:58 AM by preishuber

Comments

# re: Gridview control with dropdownlist and DBNull

Tuesday, May 23, 2006 9:26 AM by vesi
super!!!

# re: Gridview control with dropdownlist and DBNull

Tuesday, August 08, 2006 12:46 AM by KoolShen

Thank you it's work perfectly

# re: Gridview control with dropdownlist and DBNull

Tuesday, October 03, 2006 11:59 AM by drudo

The databind statement fails.  I'm using C#, but when the databind executes, it fails saying that the dropdownlist does not have a naming conainer and that the control must be added to the page before a databind.  Any suggestions?  Thanks.

# re: Gridview control with dropdownlist and DBNull

Friday, February 02, 2007 8:37 AM by Jon Byrd

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.DataBind()

                   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

       Me.Gridview_SetDataSource(Me.ddl_MainCats.SelectedValue)

       Me.GridView1.DataBind()

   End Sub

Thanks.

Jon

# re: Gridview control with dropdownlist and DBNull

Thursday, May 24, 2007 11:02 AM by Steve Brailsford

I am having a problem with a dropdown in a edittemplate. I can not get the control to initialize the list. The dropdown doesn't get created until I press Edit. Then it doesn't find it when I capture the various events.

   protected void gvDocuments_RowCreated(object sender, GridViewRowEventArgs e)

   {

       if (gvDocuments.EditIndex > -1 && e.Row.RowType == DataControlRowType.DataRow)

       {

           // If is edit mode load drop down with items

           GridViewRow gvRow = e.Row;

           DropDownList ddlDocumentStatus = (DropDownList)gvRow.FindControl("ddlDocumentStatus");

           if (ddlDocumentStatus != null)

....

ddlDocumentStatus is always null.

I also tried RowDataBound and RowEditing Events. The dropdown is never found.

# re: Gridview control with dropdownlist and DBNull

Friday, November 16, 2007 7:07 AM by Mahesh Chavan

The code is superb..

It helped me a lot....

Thanks..

Leave a Comment

(required) 
(required) 
(optional)
(required)