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  
farjack1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

farjack1Author Commented:
1st Link
i need to assign value in testGridView_RowEditing event

2nd link
there is no selectedvalue property in gridview
0
farjack1Author Commented:
i am waiting for answer

Thanks
0
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Kaushal AroraTechnical AnalystCommented:
Do you want to view the text written in label as Selected Value of the dropdown??
0
rajeeshmcaCommented:
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>
0
farjack1Author Commented:
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
0
farjack1Author Commented:
This is the Answer

Protected Sub testGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles testGridView.RowDataBound

        Dim ddlPbx As DropDownList = DirectCast(e.Row.FindControl("ddlProductName"), DropDownList)
        If Not ddlPbx Is Nothing Then
            ddlPbx.SelectedIndex = ddlPbx.Items.IndexOf(ddlPbx.Items.FindByText(strProductName))
        End If

    End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rajeeshmcaCommented:
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>
0
rajeeshmcaCommented:
Hi farjack1,

Did u try with the above said change?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.