Home > ASP.Net > Accesse DropDownLists inside the GridView Control

Accesse DropDownLists inside the GridView Control


Hi

try this example to Accesse DropDownLists inside the GridView Control

Step1 :- Create new website using VS2005 and add new web page

Step2 :- From Toolbox drag  two Gridview controls and Button control so the first Gridview to Bind with DropdownList and the second one is to Display selected value from DropdownList inside the first Gridview and display it so your web page will be like that

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
            <Columns>
                <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />
                <asp:TemplateField HeaderText="Category Name">
                    <ItemTemplate>
                        <asp:DropDownList DataSource='<%# GetCategoryNames() %>' DataTextField="CategoryName" DataValueField="CategoryName" ID="ddlCategoryName" runat="server">
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Description">
                    <ItemTemplate>
                        <asp:DropDownList DataSource='<%# GetCategoryDescriptions() %>' DataTextField="Description" DataValueField="Description" ID="ddlDescription" runat="server">
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:CommandField HeaderText="Select" ShowSelectButton="True" />
            </Columns>
            <FooterStyle BackColor="White" ForeColor="#000066" />
            <RowStyle ForeColor="#000066" />
            <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
            <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
        </asp:GridView>
        &nbsp;&nbsp;<br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Get Selected Items" /><br />
        <br />
        <asp:GridView ID="GridView2" runat="server">
        </asp:GridView>

Step3 :- in code behind add the following code


protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindData();
        }      
    }

    private void BindData()
    {
        SqlConnection myConnection = new SqlConnection(ConnectionString);
        SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM Categories", myConnection);
        DataSet ds = new DataSet();
        ad.Fill(ds, "Categories");
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }

    public DataSet GetCategoryNames()
    {
        SqlConnection myConnection = new SqlConnection(ConnectionString);
        SqlDataAdapter ad = new SqlDataAdapter("SELECT CategoryName FROM Categories", myConnection);
        DataSet ds = new DataSet();
        ad.Fill(ds, "Categories");
        return ds;
    }

    public DataSet GetCategoryDescriptions()
    {
        SqlConnection myConnection = new SqlConnection(ConnectionString);
        SqlDataAdapter ad = new SqlDataAdapter("SELECT Description FROM Categories", myConnection);
        DataSet ds = new DataSet();
        ad.Fill(ds, "Categories");
        return ds;
    }

    private string ConnectionString
    {
        get { return @"Server=localhost;Database=Northwind;Trusted_Connection=true"; }
    }
  
    // 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();

    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string categoryName = ((DropDownList)GridView1.SelectedRow.FindControl("ddlCategoryName")).SelectedItem.Value;
        Response.Write(categoryName);
    }

Hope this helps

Good Luck.

Advertisements
Categories: ASP.Net
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: