Dropdownlist in a Gridview and the default (null) value problem

To edit a value in ASP.NET 2.0 gridview control you have more options than only a textbox. If you want to choose of a valuelist you can take a dropdownlist. The data of dropdown comes usaly from a table and is placed in the edititemtemplate. A second SQLDatasource control gets the data (here named DSPLZ). The binding is done by selectedValue

<EditItemTemplate>

<asp:SqlDataSource ID="DSKonf" runat="server" ConnectionString="<%$ ConnectionStrings:EventsConnectionString %>"

SelectCommand="SELECT * FROM [AktuelleKonfSteuerung]"></asp:SqlDataSource>

</EditItemTemplate>

<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="DSPLZ" DataTextField="PLZ"

DataValueField="PLZ" SelectedValue='<%# Bind("adressplz") %>'>

</asp:DropDownList> ..

The problems come if the data in the table doesnt fit your data in the PLZ table. Most common reason is that the field contains a NULL Value. Bind fails!

The trick is that you can add items to the dropdown list by declaration. The second part of the trick is to add the DB entrys to the declarated entrys by AppenddataboundItems. Take care to set the value to "" cause the bind gives also back a "" if the field contains NULL

<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="DSPLZ" DataTextField="PLZ"

DataValueField="PLZ" SelectedValue='<%# Bind("shipPLZ") %>' AppendDataBoundItems=true>

<asp:ListItem Text="wählen" Value=""></asp:ListItem>

</asp:DropDownList>

</asp:TemplateField>

Published Sunday, August 07, 2005 1:39 PM by preishuber

Comments

# re: Dropdownlist in a Gridview and the default (null) value problem

Sunday, August 07, 2005 9:41 AM by Brendan G
Why don't you just have the select query convert NULLs into empty strings instead?

# re: Dropdownlist in a Gridview and the default (null) value problem

Sunday, August 07, 2005 9:55 AM by Hannes Preishuber
bind makes the convert, this is not the problem
The problem is that "" doenst fit to any of the entrys in the dropdownlist and so the selectedvalue can not be done

# re: Dropdownlist in a Gridview and the default (null) value problem

Wednesday, June 07, 2006 8:47 AM by Cyril
Thanx man, i was really in front of a problem with it and i didn't wanted to add a special 'EMPTY' value to my referenced table. This trick helped me ;)

# re: Dropdownlist in a Gridview and the default (null) value problem

Tuesday, June 20, 2006 3:30 PM by Graham Powell
Yeah, adding a static row to the database lookup really saved me a lot of grief.  This is really good to know.

# re: Dropdownlist in a Gridview and the default (null) value problem

Tuesday, July 04, 2006 5:43 AM by Liam

Good solution to a nasty problem.

Thanks

# re: Dropdownlist in a Gridview and the default (null) value problem

Thursday, July 06, 2006 3:20 AM by Florante Navaja

Just what i needed!

Thanks a lot man!

# re: Dropdownlist in a Gridview and the default (null) value problem

Friday, July 07, 2006 5:42 PM by kurt

Great workaround.  Thanks a lot!

# re: Dropdownlist in a Gridview and the default (null) value problem

Wednesday, August 30, 2006 3:48 AM by stricha

very good solution... now i've to search for the update problem

# re: Dropdownlist in a Gridview and the default (null) value problem

Wednesday, September 27, 2006 5:32 PM by Mark Adamson

Thanks, this worked fine for me, both for selecting and updating the data.

The wierd thing is that I tried using a more complicated solution from the MS docs and it didn't work.

Your solution seems like the intended solution of the .NET creators. It's a shame they didn't let anybody know through MSDN.

# re: Dropdownlist in a Gridview and the default (null) value problem

Monday, October 09, 2006 1:54 PM by GrassHopper

WOOOOT!!!!

WOOOOT!!!!

Big PROPS brother...

Looked along time for this, and like others tried far more complicated solutions.

YOU DA MAN!!!

# re: Dropdownlist in a Gridview and the default (null) value problem

Monday, October 16, 2006 5:46 AM by Kev Mull

HELP

I can't get it to work

I keep getting the error..

'System.Data.DataRowView' does not contain a property with the name 'StaffAutoID'

# re: Dropdownlist in a Gridview and the default (null) value problem

Tuesday, November 07, 2006 5:28 PM by Edwin

Excellent. It gave me a very good solution. Thanks.

# re: Dropdownlist in a Gridview and the default (null) value problem

Monday, November 27, 2006 9:03 AM by Sudeep

Hi Hannes Preishuber,

It worked fine for me when i added a list item like u said...

In the scenario, when i bind the dropdown to a datasource it gives the same error... the SelectedValue that i am binding to does not have a null value, also the datasource table i am binding to the dropdown does not have a null value... But the error still comes, why is that?

# re: Dropdownlist in a Gridview and the default (null) value problem

Monday, November 27, 2006 9:19 AM by Sudeep

Hi Guys,

I find one peculiar problem... probably a bug... hopefully it will help some of you...

Example :- when we have  SelectedValue  binding column "col1" which has let us say "A" for all the rows... i.e. there are no nulls... and let us assume the datasource of the dropdownlist itself has three values say "A","B","C"... which means "A" is there in the datasource list...

It gives the error... the fix is to add something like this to the list items

<asp:ListItem Text="A" Value="A"></asp:ListItem> and it starts working!!!! and AppendDataBoundItems=true should also be there...

# re: Dropdownlist in a Gridview and the default (null) value problem

Tuesday, December 12, 2006 12:21 AM by jedi_aka

What I can't Believe is that is that Microsoft still hasn't figured out that it is one of the main way to use a datagrid.

The Ruby on Rails team has figured it out.

# re: Dropdownlist in a Gridview and the default (null) value problem

Wednesday, January 03, 2007 6:21 PM by Fred

You are the Man.. you defenitly saved my a**,

I was  trying all sorts of solutions and non was working.. Still therer must be a lttle trick, so we can avoid to add manually all the possible values taht may come from the database and are not in the Bound dropdown.. Hope someone can help

# re: Dropdownlist in a Gridview and the default (null) value problem

Friday, January 12, 2007 9:00 AM by Raj

Awesome.... Thanks a ton....

# Template Field in Gridview

Friday, February 02, 2007 1:53 PM by Frank_t

I have created a basic gridview and with one column defined as a template field. The template field contains a dropdown.  The problem is the header text and dropdown are placed outside the gridview grid.  Any ideas on why this is would be greatly appreciated.

<asp:Gridview ID="Composite" runat="server" DataKeyNames="testkey" Style="z-index: 108;

           left: 224px; position: absolute; top: 68px" AutoGenerateColumns="False" Width="900px" >

           <Columns>

               <asp:BoundField DataField="method" HeaderText="METHOD" readonly="True">

                   <HeaderStyle width="65px" />

               </asp:BoundField>

               <asp:BoundField DataField="lab" HeaderText="LAB" readonly="True" >

                   <HeaderStyle width="40px" />

               </asp:BoundField>

               <asp:BoundField DataField="testkey" HeaderText="TESTKEY"  readonly="True">

                   <HeaderStyle width="65px" />

               </asp:BoundField>

               <asp:BoundField DataField="labseq" HeaderText="SEQ" readonly="True" >

                   <HeaderStyle width="40px" />

               </asp:BoundField>

               <asp:TemplateField HeaderText="STAND">

                   <EditItemTemplate>

                       <asp:DropDownList ID="ddlApparatus" runat="server"  datatextfield="Apparatus" datavaluefield="Apparatus" datasourceid="SqlApparatus" Style="z-index: 100; left: 12px;

                           position: absolute; top: 228px" Width="100px">

                       </asp:DropDownList>

                   </EditItemTemplate>

                   <HeaderStyle width="100px" />

                   <HeaderTemplate>

                       <asp:Label ID="Label3" runat="server" Style="z-index: 100; left: 12px; position: absolute;

                           top: 316px" Text="Stand"></asp:Label>

                   </HeaderTemplate>

                   <ControlStyle Width="100px" />

                   <ItemStyle Width="100px" />

               </asp:TemplateField>

               <asp:BoundField DataField="val" HeaderText="VAL" >

                   <HeaderStyle width="40px" />

                   </asp:BoundField>

                <asp:BoundField DataField="com1" HeaderText="COM1" >

                   <HeaderStyle width="40px" />

               </asp:BoundField>

               <asp:BoundField DataField="com2" HeaderText="COM2" >

                   <HeaderStyle width="40px" />

               </asp:BoundField>

               <asp:BoundField DataField="com3" HeaderText="COM3" >

                   <HeaderStyle width="40px" />

               </asp:BoundField>

               <asp:BoundField DataField="com4" HeaderText="COM4" >

                   <HeaderStyle width="40px" />

               </asp:BoundField>

                <asp:BoundField DataField="com5" HeaderText="COM5" >

                   <HeaderStyle width="40px" />

               </asp:BoundField>

               </Columns>

       </asp:Gridview>

# re: Dropdownlist in a Gridview and the default (null) value problem

Tuesday, February 20, 2007 12:55 PM by Zohair Ahmed

I have a dropdownlist in a gridview Update, As data Inserted already in the field is Null, I want to handle this Exception, Any Clue

The problem "" doenst fit to any of the entrys in the dropdownlist and so the selectedvalue can not be done

# re: Dropdownlist in a Gridview and the default (null) value problem

Wednesday, March 14, 2007 3:52 PM by Alex

Thanks for the tip.  I've been pulling my hair out over this!

# re: Dropdownlist in a Gridview and the default (null) value problem

Friday, March 23, 2007 3:44 PM by Jake

Excellent solution to a real pain in the ass issue. Long ago I can remember I used to add static rows to lookup tables that said something like "N/A"... ugh...

I like this better than performing CASE conversions on null fields in the select queries too, that way the database can actually return valid data and the application can dtermine how to interpret it.

# re: Dropdownlist in a Gridview and the default (null) value problem

Thursday, April 05, 2007 7:54 AM by Svein Folkeson

This worked almost fine for me too. My value field is an int in the database. I therefore had to set the value="0" instead in the listitem definition. Then it worked just fine. Thanks a lot.

# re: Dropdownlist in a Gridview and the default (null) value problem

Sunday, June 03, 2007 12:48 PM by alberto

Grande soluzione. Grazie 1K from Italy

# re: Dropdownlist in a Gridview and the default (null) value problem

Wednesday, June 13, 2007 5:23 AM by fran

I use Data Access Layer so, for null value I have to define integer variable as nullable eg. ?category. Then, to select, update or insert this value throught dropdownlist i had to append nullable item throught code. Regards

# re: Dropdownlist in a Gridview and the default (null) value problem

Wednesday, June 13, 2007 7:05 AM by Newb

How to get controlid of the Dropdownlist that is inside the Gridview.

I use the control id in Update Parameter..

# re: Dropdownlist in a Gridview and the default (null) value problem

Wednesday, July 25, 2007 6:48 AM by Velu

Heartly thanks for ur greate help.

Regards

Velu s

# re: Dropdownlist in a Gridview and the default (null) value problem

Friday, August 03, 2007 3:19 AM by KJ

Awesome! Thanks a million.

# re: Dropdownlist in a Gridview and the default (null) value problem

Thursday, August 16, 2007 10:28 PM by Doug Vanderweide

You'd think VS 2005 would be kind enough to include the SelectedValue attribute / parameter in autocomplete. You'd be wrong, but you'd think that.

Thanks much for the help!

# re: Dropdownlist in a Gridview and the default (null) value problem

Thursday, August 30, 2007 9:13 AM by Mike Kingscott

Ding! Thanks a bunch, that's saved me a lot of searching...

# re: Dropdownlist in a Gridview and the default (null) value problem

Monday, September 17, 2007 7:54 PM by Aumar G. Daguia

It works for me too! Thanks!

# re: Dropdownlist in a Gridview and the default (null) value problem

Tuesday, October 02, 2007 2:07 PM by 3 sheets to the wind

a facile solution to a complex problem.  cheers!

# re: Dropdownlist in a Gridview and the default (null) value problem

Wednesday, October 03, 2007 2:39 AM by Mjr. Malfunkshun

Ditto on the thanx and praise, a simple, elegant solution.

# re: Dropdownlist in a Gridview and the default (null) value problem

Wednesday, October 17, 2007 4:09 PM by Grogie

Thanks! Worked great

# re: Dropdownlist in a Gridview and the default (null) value problem

Thursday, October 18, 2007 4:50 PM by Efi

Wow this was the solution to my issue I had fixed the data I didnt find the error in the code, that was crazy! Thanks so much!

# re: Dropdownlist in a Gridview and the default (null) value problem

Thursday, October 25, 2007 12:36 AM by Wasim

Your example code for both these issues saved my day!

Thanks!

# re: Dropdownlist in a Gridview and the default (null) value problem

Sunday, November 04, 2007 1:58 AM by subbu

hai this is nice. i had a problem with templeate field in the grid view. when i created dropdownlist in the template field i can not handle the selectindex or other events of the drop down list when it is post back. if any solutions plz send the answer.

Thank you in advance.

subbu

# re: Dropdownlist in a Gridview and the default (null) value problem

Saturday, November 10, 2007 7:49 PM by Greg

Great solution, however I was still scratching my head using Microsoft Access as the data source. The issue is that if you create related columns in an Access table using the Lookup Wizard, the "Default Value" for that column becomes 0. Access then stores 0 instead of Null for all new records, however they look identical in a table view.

I had to query the table where lookupID = 0 or where lookupID is Null to spot the difference.

I have spent several hours scratching my head as to why some of my nulls were selected as 0 and others as DBNull.

# re: Dropdownlist in a Gridview and the default (null) value problem

Tuesday, November 13, 2007 8:20 AM by Barkhan

I've the same problem but your solution didn't work.

codes;

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "www.w3.org/.../xhtml1-transitional.dtd">

<html xmlns="www.w3.org/.../xhtml" >

<head runat="server">

   <title>Untitled Page</title>

</head>

<body>

   <form id="form1" runat="server">

       &nbsp;<div>

       <asp:FormView ID="FormView1" runat="server" AllowPaging="True" DataKeyNames="farmcode"

           DataSourceID="SqlDataSource1">

           <EditItemTemplate>

               farmcode:

               <asp:Label ID="farmcodeLabel1" runat="server" Text='<%# Eval("farmcode") %>'></asp:Label><br />

               fname:

               <asp:TextBox ID="fnameTextBox" runat="server" Text='<%# Bind("fname") %>'></asp:TextBox><br />

               fsurname:

               <asp:TextBox ID="fsurnameTextBox" runat="server" Text='<%# Bind("fsurname") %>'></asp:TextBox><br />

               Country:

               <asp:DropDownList ID="CountryDDL" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1"

                   DataTextField="Country" DataValueField="CID">

               </asp:DropDownList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Cnn %>"

                   SelectCommand="SELECT [CID], [Country] FROM [Country]"></asp:SqlDataSource>

               <br />

               Province:

               <asp:DropDownList ID="ProvinceDDL" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource2"

                   DataTextField="Province" DataValueField="PID">

               </asp:DropDownList><asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:Cnn %>"

                   SelectCommand="SELECT [PID], [Province] FROM [Province] WHERE ([CID] = @CID)">

                   <SelectParameters>

                       <asp:ControlParameter ControlID="CountryDDL" Name="CID" PropertyName="SelectedValue"

                           Type="Decimal" />

                   </SelectParameters>

               </asp:SqlDataSource>

               <br />

               Area:

               <asp:DropDownList ID="AreaDDL" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource3"

                   DataTextField="Area" DataValueField="AID">

               </asp:DropDownList><asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:Cnn %>"

                   SelectCommand="SELECT [AID], [Area] FROM [Area] WHERE ([PID] = @PID)">

                   <SelectParameters>

                       <asp:ControlParameter ControlID="ProvinceDDL" Name="PID" PropertyName="SelectedValue"

                           Type="Decimal" />

                   </SelectParameters>

               </asp:SqlDataSource>

               <br />

               Village:

               <asp:DropDownList ID="VillageDDL" runat="server" DataSourceID="SqlDataSource4" DataTextField="Village"

                   DataValueField="VID"  AppendDataBoundItems="True" DataMember="DefaultView">

                  <asp:ListItem Text="A" Value="A"></asp:ListItem>

               </asp:DropDownList><asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:Cnn %>"

                   SelectCommand="SELECT [VID], [Village] FROM [Village] WHERE ([AID] = @AID)">

                   <SelectParameters>

                       <asp:ControlParameter ControlID="AreaDDL" Name="AID" PropertyName="SelectedValue"

                           Type="Decimal" />

                   </SelectParameters>

               </asp:SqlDataSource>

               <br />

               phone:

               <asp:TextBox ID="phoneTextBox" runat="server" Text='<%# Bind("phone") %>'></asp:TextBox><br />

               contract:

               <asp:TextBox ID="contractTextBox" runat="server" Text='<%# Bind("contract") %>'></asp:TextBox><br />

               opinion:

               <asp:TextBox ID="opinionTextBox" runat="server" Text='<%# Bind("opinion") %>'></asp:TextBox><br />

               street:

               <asp:TextBox ID="streetTextBox" runat="server" Text='<%# Bind("street") %>'></asp:TextBox><br />

               <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"

                   Text="Update"></asp:LinkButton>

               <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"

                   Text="Cancel"></asp:LinkButton>

           </EditItemTemplate>            

           <ItemTemplate>

               farmcode:

               <asp:Label ID="farmcodeLabel" runat="server" Text='<%# Eval("farmcode") %>'></asp:Label><br />

               fname:

               <asp:Label ID="fnameLabel" runat="server" Text='<%# Bind("fname") %>'></asp:Label><br />

               fsurname:

               <asp:Label ID="fsurnameLabel" runat="server" Text='<%# Bind("fsurname") %>'></asp:Label><br />

               Village:

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

               <asp:Label ID="VillageLabel" runat="server" Text='<%# Bind("Village") %>'></asp:Label><br />

               Area:

               <asp:Label ID="AreaLabel" runat="server" Text='<%# Bind("Area") %>'></asp:Label><br />

               Province:

               <asp:Label ID="ProvinceLabel" runat="server" Text='<%# Bind("Province") %>'></asp:Label><br />

               Country:

               <asp:Label ID="CountryLabel" runat="server" Text='<%# Bind("Country") %>'></asp:Label><br />

               phone:

               <asp:Label ID="phoneLabel" runat="server" Text='<%# Bind("phone") %>'></asp:Label><br />

               contract:

               <asp:Label ID="contractLabel" runat="server" Text='<%# Bind("contract") %>'></asp:Label><br />

               opinion:

               <asp:Label ID="opinionLabel" runat="server" Text='<%# Bind("opinion") %>'></asp:Label><br />

               street:

               <asp:Label ID="streetLabel" runat="server" Text='<%# Bind("street") %>'></asp:Label><br />

               <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"

                   Text="Edit"></asp:LinkButton>

           </ItemTemplate>

       </asp:FormView>

       <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Cnn %>"

           SelectCommand="SELECT     dbo.farmers.farmcode, dbo.farmers.fname, dbo.farmers.fsurname, dbo.farmers.VID, dbo.Village.Village, dbo.Area.Area, dbo.Province.Province, dbo.Country.Country, dbo.farmers.phone, &#13;&#10;                      dbo.farmers.contract, dbo.farmers.opinion, dbo.farmers.street&#13;&#10;FROM         dbo.Country INNER JOIN&#13;&#10;                      dbo.Province ON dbo.Country.CID = dbo.Province.CID INNER JOIN&#13;&#10;                      dbo.Area ON dbo.Province.PID = dbo.Area.PID INNER JOIN&#13;&#10;                      dbo.Village ON dbo.Area.AID = dbo.Village.AID INNER JOIN&#13;&#10;                      dbo.farmers ON dbo.Village.VID = dbo.farmers.VID"

           UpdateCommand="UPDATE farmers SET fname = @fname, fsurname = @fsurname, VID = @Village, street = @street, phone = @phone, contract = @contract, opinion = @opinion WHERE (farmers.farmcode = @farmcode)">

           <UpdateParameters>

               <asp:FormParameter Name="fname" FormField="fname" />

               <asp:FormParameter Name="fsurname" FormField="fsurname" />            

               <asp:FormParameter Name="Village" FormField="VillageID" />

               <asp:FormParameter Name="street" FormField="street" />

               <asp:FormParameter Name="phone" FormField="phone" />

               <asp:FormParameter Name="contract" FormField="contract" />

               <asp:FormParameter Name="opinion" FormField="opinion" />

               <asp:FormParameter Name="farmcode" FormField="farmcode" />              

           </UpdateParameters>

       </asp:SqlDataSource>

   </div>

   </form>

</body>

</html>

# re: Dropdownlist in a Gridview and the default (null) value problem

Tuesday, March 18, 2008 6:03 PM by missy

This is exactly whatI needed!  Thank you.

# re: Dropdownlist in a Gridview and the default (null) value problem

Tuesday, April 22, 2008 10:29 AM by Rob Blij

Thanks for a great solution!

# re: Dropdownlist in a Gridview and the default (null) value problem

Wednesday, April 30, 2008 8:27 AM by vamsi

Good solution ....

# re: Dropdownlist in a Gridview and the default (null) value problem

Monday, June 23, 2008 2:49 AM by Savita

i m editing my gridview Using dropdown list as edit template filed values to the ddl are fetched from the database plz tel me after click on update button my database is updated but the dropdown are still visible how do i place lable in place of dropdownlist after update showing my updated details in the gridview

# re: Dropdownlist in a Gridview and the default (null) value problem

Monday, October 20, 2008 1:34 PM by Luciano Carvalho

Show, muito bom o truque, vale todos os agradecimentos acima... I am from Brazil.

Guy, very smart your trick...

# re: Dropdownlist in a Gridview and the default (null) value problem

Thursday, November 13, 2008 10:59 PM by Matthew Kruskamp

Worked great. Thanks for the help.

# re: Dropdownlist in a Gridview and the default (null) value problem

Wednesday, February 25, 2009 5:13 AM by Jayakumar

Thanks men. Its really worked well.

# re: Dropdownlist in a Gridview and the default (null) value problem

Sunday, March 01, 2009 1:26 PM by David

Thank you, very subtle and quick.

Welcome to my thesis citations.

# re: Dropdownlist in a Gridview and the default (null) value problem

Thursday, July 02, 2009 11:32 AM by Paul

It worked for me! Thanks! I was killing my self trying to get a solution to this problem.

Leave a Comment

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