Click here to Skip to main content
15,913,587 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Guys i wan to Bind Dropdownlist Datatexfield On Text box
C#
protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
 {
     foreach (GridViewRow row in GridView1.Rows)
     {
         if (row.RowIndex == GridView1.SelectedIndex) {
         TABLEzemota.Visible = true;

        
         Label456456.Text = (row.FindControl("DropDownList5") as Label).Text; <<< this one 

         }
         else
         {
             
         }
     }

 }

can you help meee how i can make it
Posted

1 solution

Hello, You need to do as below if i understood your question.

Label456456.Text= ((DropDownList)row.FindControl("DropDownList5")).SelectedItem.Value;

Do let me know if this works for you !!!
 
Share this answer
 
Comments
GTR0123 30-Dec-15 3:28am    
Object reference not set to an instance of an object.
this error
adityaswami89 30-Dec-15 3:37am    
Is there any item selected in that dropdown ?

There are 2 aspects :
1. IF you want to bind then you can do something as below :

<asp:DropDownList ID="ddlState" runat="server" DataSourceID="sqlDatasource1"
DataTextField="State" AppendDataBoundItems="True"
SelectedValue='<%# Eval("State") %>' DataValueField="State">


Refer below link :
https://msdn.microsoft.com/en-us/library/ms178294.aspx

2. Intending to assign the selected value of a dropdown present in a gridview into the a label (as you are trying the solution above , i assumed you are only assigning) , you can do as below :

Label456456.Text= ((DropDownList)row.FindControl("DropDownList5")).SelectedItem.Value;

This would work. Kindly debug !!!
GTR0123 30-Dec-15 4:16am    
nah this doesn't works me When i quick watched Label456456.Text= ((DropDownList)row.FindControl("DropDownList5")).SelectedItem.Value; << this one on Label there is nothing in text and On DropDownlist5 it is telling me that it isn't existing buttt i have this
<asp:DropDownList
ID="DropDownList5"
runat="server"
DataSourceID="ObjectDataSource3"
DataTextField="Name"
DataValueField="CODE2"
SelectedValue='<%# Bind("City_id") %>'
CssClass="mystyle"
>

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