Bhavya Dhiman
Bhavya Dhiman

Reputation: 30

DropDownList in a GridView

Suppose I have a Gridview having some rows. i have two columns containing dropdown lists. My question is to find the RowIndex of a GridView when SelectedIndexChanged Event of GridView occurs of one dropdown list such that i can apply the functionality of another dropdown list in a same row(Read the comment too). Sample code is listed here:

protected void dropdown1_SelectedIndexChanged(object sender, EventArgs e)
{
    // I want the row index so that i can apply the functionality in same row
    // If this doesn't work, any alternative ?
}


protected void func()
{
    DataTable dt = (DataTable)ViewState["CurrentTable"]
    for(int i = 0; i<dt.Rows.Count; i++) 
    {
        DropdownList d1 = (DropDownList)GridView1.Rows[i].Cells[0].FindControl("dropdown1");
        DropdownList d2 = (DropDownList)GridView1.Rows[i].Cells[1].FindControl("dropdown2");
    }
}

Upvotes: 0

Views: 67

Answers (2)

Martin Parenteau
Martin Parenteau

Reputation: 73721

You can get the GridView row this way:

protected void dropdown1_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList dropdown1 = sender as DropDownList;
    GridViewRow row = dropdown1.NamingContainer as GridViewRow;
    // Do something with row or with row.RowIndex
}

Upvotes: 1

Barry O&#39;Kane
Barry O&#39;Kane

Reputation: 1187

Change EventArgs to DataGridViewCellEventArgs and you'll be able to get the row from e

So your method becomes:

protected void dropdown1_SelectedIndexChanged(object sender, EventArgs e)
{
    int row = e.RowIndex;
}

Upvotes: 0

Related Questions