PriceCheaperton
PriceCheaperton

Reputation: 5349

how to change drop down list based on gridview selection

I have a drop down list which will load up with a series of customer ID numbers. I then have a gridview control on my page with the select hyperlink. When i click on the link to select the row in gridview i would like my dropdown to change to that number.

Below is what i have tried but doesn't work:

for (int i = 0; i < GridView1.Rows.Count; i++)
{
    if (!GridView1.SelectedIndex.Equals(-1))
    {
        DropDownList ddl;

        ddl = (DropDownList)form1.FindControl("ddl_Customers");
        ddl.SelectedValue = (String)GridView1.SelectedDataKey.Values[0];
    }
}

Upvotes: 0

Views: 91

Answers (2)

renakre
renakre

Reputation: 8291

You can directly use GridView1.SelectedValue.ToString() for this. To use that, you should define a datakeyname like this: <asp:Gridview DataKeyNames="CustomerID">

Then all you need is this:

void GridView1_SelectedIndexChanged(Object sender, EventArgs e) {
    ddl_Customers.SelectedValue = GridView1.SelectedValue.ToString();
}

Upvotes: 0

kavun
kavun

Reputation: 3381

Handle the SelectedIndexChanged event for GridView1

void GridView1_SelectedIndexChanged(Object sender, EventArgs e) {
    ddl_Customers.SelectedValue = GridView1.SelectedDataKey.Value.ToString();
}

Upvotes: 1

Related Questions