Click here to Skip to main content
16,021,004 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hello

On my webpage when I edit a row from the gridview, a dropdownlist is used to select a value. Now the default selected value is the first value from the list provided by the datasource.
The problem I have is that I can't set the selected value for the DDL.

In my vb code I try to set the selected value as follows:
VB
Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs) Handles GridView1.RowEditing
    Dim row As GridViewRow
    Dim lstdrop As DropDownList
    row = GridView1.Rows.Item(e.NewEditIndex())
    lstdrop = row.FindControl("DropDownListOwner")
    lstdrop.SelectedIndex = 3
End Sub


Here is a part of my asp code:
XML
<EditItemTemplate>
    <asp:DropDownList ID="DropDownListOwner" runat="server" DataSourceID="SqlDataOwner" DataValueField="User_name" DataTextField="User_name" AutoPostBack="true">
    </asp:DropDownList>
</EditItemTemplate>


When clicking edit on a row the following error occurs:
System.NullReferenceException: Object reference not set to an instance of an object
Posted
Updated 3-Jun-16 6:57am

I think this might help.

Try adding the following values to the EditItemTemplate DropDownList item.

SelectedValue='<%#Bind("value")%>' AppendDataBoundItems=true
 
Share this answer
 
Comments
tomtimmers 29-Mar-11 9:02am    
I've changed it to:
<asp:DropDownList ID="DropDownListOwner" runat="server" DataSourceID="SqlDataOwner" DataValueField="UserID" DataTextField="User_name" AutoPostBack="true" AppendDataBoundItems="True" SelectedValue='<%# Bind("User_name") %>'>
And now I get the error: "'System.Data.DataRowView' does not contain a property with the name 'User_name'"
Another thing is that in Microsoft Visual Web Developer 2010 Express the property 'SelectedValue' isn't suggested. Is this normal?
tomtimmers 29-Mar-11 9:07am    
Is it possible to set the SelectedValue to a value from the list? Like this:
<asp:DropDownList ID="DropDownListOwner" runat="server" DataSourceID="SqlDataOwner" DataValueField="UserID" DataTextField="User_name" AutoPostBack="true" AppendDataBoundItems="True" SelectedValue="ttimmers">
If I do this I get the error:
The 'SelectedValue' property cannot be set declaratively
tomtimmers 30-Mar-11 4:50am    
Your suggestion works.
I modified the DDL as follows:
<asp:DropDownList ID="DropDownListOwner" runat="server" DataSourceID="SqlDataOwner" DataValueField="User_name" DataTextField="User_name" AutoPostBack="true" AppendDataBoundItems="true" SelectedValue='<%#Bind("Pool_IP_owner")%>'>
Member 9124526 10-Oct-14 0:04am    
Hi

I am facing same Issue, how you got SelectedValueProperty in DropDownList as I am not finding the SelectedValue property
ZeeroC00l 30-Mar-11 12:12pm    
Good to know that the suggestion helped you :)
Not sure if it matters, but try removing the parenthesis on NewEditIndex
 
Share this answer
 
Comments
tomtimmers 29-Mar-11 5:55am    
Thanks for the suggestion, but the result is the same.
walterhevedeich 29-Mar-11 6:04am    
Are you using master page/user control(ascx) for this one?
tomtimmers 29-Mar-11 7:22am    
I'm not using ascx. I have a User_ReleaseIP.aspx file that uses a User_ReleaseIP.aspx.vb file.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900