Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,
I have a grid view.Inside grid view i have taken template field and i have put drop down list in it.I am filling drop down list on row data bound event.Now my problem is that i want select items value on selected index change of drop down list.so when i select any item from drop down list how to get its selected value.



coding of filling drop down inside grid view
C#
 DataRowView r = e.Row.DataItem as DataRowView;
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                   
                        DropDownList drptype1;
                        drptype1 = (DropDownList)(e.Row.FindControl("drpitem"));
                        DataSet fill = objitem.get_all_details();
                        drptype1.DataSource = fill.Tables[0];
                        drptype1.DataTextField = "item_name";
                        drptype1.DataValueField = "item_id";
                        drptype1.DataBind();
                        drptype1.Items.Insert(0, new ListItem("Select", "-1"));
}

so how to get its selected value
Posted
Updated 29-Sep-10 0:52am
v2
Comments
Hiren solanki 29-Sep-10 6:53am    
added 'pre' tags for code visibility.

what is drptype1.SelectedItem.Value returning
 
Share this answer
 
Comments
Hiren solanki 29-Sep-10 6:32am    
is that a answer or another question ?, put your question in comment rather then in answer section.
I assume you want an event handler method to be executed in the CodeBehind when user selects a value in the DropDownList inside the GridView. To do this, I have the following suggestions:

After the line

DropDownList drptype1;
drptype1 = (DropDownList)(e.Row.FindControl("drpitem"));


Add the following line of code to add a SelectedIndexChanged event handler to the DropDownList:

SQL
drptype1.SelectedIndexChanged += new EventHandler(DropDownList_SelectedIndexChanged);


And, define the EventHandler DropDownList_SelectedIndexChanged in the CodeBehind as follows:

C#
void DropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
        DropDownList ddl = sender as DropDownList;
        if (ddl != null)
        {
            string selectedValue = ddl.SelectedItem.Value;
            string selectedText = ddl.SelectedItem.Text;
        }
}
 
Share this answer
 

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