Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.
Some developer emailed me asking that how can he extract the selected value from a DropDownList which is inside the GridView control. Over here I will present two ways to extract the values. The first way loops through the GridView control and gets all the values from all the rows (Rows that contains the DropDownList) and binds them to the DataTable object and later to the another GridView control. Second method simply uses the feature of the select column and displays the DropDownList value from the selected row. Getting all the Selected Rows from the GridView Control: This code is only to get the value of the DropDownList control. You will need to fill the DropDownList yourself (I will cover this in my article).
 // Gets the selected items in the GridView Control
    protected void Button1_Click(object sender, EventArgs e)
    {
        string categoryName = String.Empty;
        string description = String.Empty;

        DataTable dt = new DataTable();
        dt.Columns.Add("CategoryName");
        dt.Columns.Add("Description");

        foreach (GridViewRow row in GridView1.Rows)
        {
            categoryName = ((DropDownList)row.FindControl("ddlCategoryName")).SelectedItem.Value;
            description = ((DropDownList)row.FindControl("ddlDescription")).SelectedItem.Value;

            DataRow dr = dt.NewRow();
            dr["CategoryName"] = categoryName;
            dr["Description"] = description;
            dt.Rows.Add(dr);

        }      

        GridView2.DataSource = dt;
        GridView2.DataBind(); 

    }

Getting value of the selected row in GridView:
 protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string categoryName = ((DropDownList)GridView1.SelectedRow.FindControl("ddlCategoryName")).SelectedItem.Value;
        Response.Write(categoryName);
    }

Posted on Saturday, November 5, 2005 10:51 PM | Back to top


Comments on this post: DropDownLists in the GridViewControl

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net