Click here to Skip to main content
15,850,921 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Dear Sir,
I have a doubt in asp.net c# Gridview. When i click a dropdownlist, i select any one of the data,i need to bind selected value to Gridview label control. This process only will happen on (protected void ddlclassification_description_SelectedIndexChanged(object sender, EventArgs e)) this event. Here i pasted my code below.
C#
protected void ddlclassification_description_SelectedIndexChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < gvspec.Rows.Count; i++)
        {
            DropDownList ddlvalue = (DropDownList)gvspec.Rows[i].FindControl("ddlclassification_description");
            Label lblddlclasscode = (Label)gvspec.Rows[i].FindControl("classification_code");       
            string val = ddlvalue.SelectedValue.ToString();
        }
    }
Posted
Updated 24-Sep-12 19:21pm
v2
Comments
So what problem you are facing now in the implementation ?

1 solution

Following is sample HTML

<asp:gridview id="GridView1" runat="server" showheader="true" autogeneratecolumns="false" xmlns:asp="#unknown">
onrowdatabound="GridView1_RowDataBound">
<columns> <asp:templatefield headertext="ddl_test">
<itemtemplate>
<asp:dropdownlist runat="server" id="ddl_test" autopostback="true">
OnSelectedIndexChanged="ddlTest_SelectedIndexChanged">


<columns> <asp:templatefield headertext="lbl_test">
<itemtemplate>
<asp:label runat="server" id="lbl_test">





Following is sample code behind

DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = CreateSampleData();
GridView1.DataBind();
}
}

DataTable CreateSampleData()
{
dt = new DataTable();
dt.Columns.Add("Name");
DataRow dr = dt.NewRow();
dr["Name"] = "A";
dt.Rows.Add(dr);

dr = dt.NewRow();
dr["Name"] = "B";
dt.Rows.Add(dr);

dr = dt.NewRow();
dr["Name"] = "C";
dt.Rows.Add(dr);

return dt;
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DropDownList ddl = (DropDownList)(e.Row.FindControl("ddl_test"));
if (ddl != null)
{
ddl.DataSource = dt;
ddl.DataValueField = "Name";
ddl.DataTextField = "Name";
ddl.DataBind();
}
}

protected void ddlTest_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddl.NamingContainer;
Label lbl = (Label)row.FindControl("lbl_test");
lbl.Text = 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