Link to home
Start Free TrialLog in
Avatar of farjack1
farjack1Flag for United States of America

asked on

gridview dropdownlist selected value problem

I am using framework 3.5
i have a gridview its working good in this i have template column having dropdownlist when i am clicking Edit button then dropdownlist come but the value that was in the label in the same cell do not select from dropdownlist please tell me code example.

Thanks  
Avatar of meetingexpectations
meetingexpectations

Avatar of farjack1

ASKER

1st Link
i need to assign value in testGridView_RowEditing event

2nd link
there is no selectedvalue property in gridview
i am waiting for answer

Thanks
Do you want to view the text written in label as Selected Value of the dropdown??
Avatar of rajeeshmca
HI farjack1,

You can assign the SelectedValue property like

<asp:TemplateField HeaderText="Test1">
                        <ItemTemplate>
                            <asp:Label ID="TestLabel" runat="server" Text='<%# Bind("ItemLabel") %>'>
                            </asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="EditDdl" runat="server" SelectedValue='<%# Bind("BindValue") %>' >
                            </asp:DropDownList>
                        </EditItemTemplate>
                    </asp:TemplateField>
Actully i mentioned above SelectedValue property is not avaiable in

<EditItemTemplate>
                        <asp:DropDownList id="ddlProductName" runat="server"
                             DataTextField="ProductName" DataValueField="ID" DataSource='<%# loadProducts %>'
                               
                             />
                    </EditItemTemplate>
if i put that SelectedValue='<%# Bind("ProductName") %>' under dropdownlist tag then runtime error comes

System.Web.HttpException was unhandled by user code
  ErrorCode=-2147467259
  Message="DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'ProductName'."
  Source="System.Web"
  StackTrace:
       at System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName)
       at System.Web.UI.DataBinder.Eval(Object container, String[] expressionParts)
       at System.Web.UI.DataBinder.Eval(Object container, String expression)
       at System.Web.UI.TemplateControl.Eval(String expression)
       at ASP.default_aspx.__DataBinding__control10(Object sender, EventArgs e) in C:\working\ASPProject\Default.aspx:line 21
       at System.Web.UI.Control.OnDataBinding(EventArgs e)
       at System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e)
       at System.Web.UI.WebControls.ListControl.PerformSelect()
       at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
       at System.Web.UI.Control.DataBindChildren()
  InnerException:

if i remove  SelectedValue='<%# Bind("ProductName") %>' then works fine but value not selected as required
ASKER CERTIFIED SOLUTION
Avatar of farjack1
farjack1
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi farjack1,

U have given the wrong field to bind for the SelectedValue property...

Change it to

EditItemTemplate>
       <asp:DropDownList id="ddlProductName" runat="server"
       DataTextField="ProductName" DataValueField="ID" DataSource='<%# loadProducts %>'
       SelectedValue ='<%# Bind("ID") %>'   />
</EditItemTemplate>
Hi farjack1,

Did u try with the above said change?