---
title: "how to get values in dropdownlist bounded within a gridview?"  
description: "how to get values in dropdownlist bounded within a gridview?"  
author: "Anonymous User"  
published: 2014-11-20  
updated: 2014-11-20  
canonical: https://www.mindstick.com/forum/12659/how-to-get-values-in-dropdownlist-bounded-within-a-gridview  
category: "asp.net"  
tags: ["asp.net", "gridview", "dropdown"]  
reading_time: 2 minutes  

---

# how to get values in dropdownlist bounded within a gridview?

Inside [gridview](https://www.mindstick.com/articles/873/update-delete-edit-gridview-in-asp-dot-net) a [dropdownlist](https://www.mindstick.com/articles/324837/how-to-bind-dropdownlist-in-mvc-core-from-database-using-entity-framework) is there

```
<asp:TemplateField HeaderText="Quantity">    <ItemTemplate>        <asp:DropDownList ID="quantity" runat="server" DataValueField="ItemID" DataTextField="Quantity">        </asp:DropDownList>    </ItemTemplate></asp:TemplateField>
```

for each itemid there is some numeric [value](https://www.mindstick.com/articles/23219/an-optimized-description-adds-value-to-experience-and-in-turn-effectively-guest-posting-packages) in Quantity field of [Database Table](https://www.mindstick.com/forum/159366/how-to-test-regular-expressions-in-sql-without-using-database-table) i want that this [dropdown](https://www.mindstick.com/articles/263/ajax-toolkit-dropdownextender-in-asp-dot-net) must contain the [values](https://www.mindstick.com/forum/327/sum-textbox-values) from 1 to quantity.DataTextField for all the items [present](https://answers.mindstick.com/qa/96635/explain-about-the-various-features-present-in-ms-access) in the cart

the [procedure](https://www.mindstick.com/forum/32/stored-procedure-return-datatype) by which gridview is bound is

```
create proc [dbo].[prcItemsinCart](@CartID int)as    select distinct ct.Price,ct.Quantity,ct.ItemID,           ct.CartID,ct.ProductID,p.ProductName,           isnull((  select top 1 convert(varchar,PhotoID,10) + '.' + ExtName                 from ProductPhoto                 where ProductID = ct.ProductID ),'NoImage.jpg'           ) as Product,           ( Select Price*Quantity             from CartItems             where CartID=ct.CartID and ProductID=ct.ProductID           ) as SubTotal    from CartItems as ct    inner join ProductInfo as p on ct.ProductID=p.ProductID    inner join ProductPhoto as pp on ct.ProductID=pp.ProductID    where ct.CartID=@CartID
```

## Replies

### Reply by Sumit Kesarwani

Hi Goti, \

I would add a HiddenField in the TemplateField to hold the CartID, I will use the CartID in the code. My Markup should look like:

```
<asp:TemplateField HeaderText="Quantity">    <ItemTemplate>         <asp:HiddenField ID="hdnId" runat="server" value='<%#Eval("CartID") %>'></asp:HiddenField>                                   <asp:DropDownList ID="quantity" runat="server" DataValueField="ItemID" DataTextField="Quantity">        </asp:DropDownList>    </ItemTemplate></asp:TemplateField>
```

In the code, in GridView's RowDataBound I am finding the DropDownList and HiddenField, running a Sql Query to bring my expected data, and binding them to my DropdownList:

```
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e){    if (e.Row.RowType == DataControlRowType.DataRow)    {        DropDownList quantity = e.Row.FindControl("quantity") as DropDownList;        HiddenField hdnId = e.Row.FindControl("hdnId") as HiddenField;         if (quantity != null && hdnId != null)        {            string queryString = String.Format("SELECT ItemID, Quantity FROM  CartItems  WHERE CartID= {0}", hdnId.Value);             //MyConnectionString is your connection string in web.config            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ToString()))            {                SqlCommand command = new SqlCommand(queryString, connection);                connection.Open();                SqlDataReader reader = command.ExecuteReader();                quantity.DataSource = reader;                quantity.DataValueField = "ItemID";                quantity.DataTextField = "Quantity";                quantity.DataBind();                          }        }           }}
```


---

Original Source: https://www.mindstick.com/forum/12659/how-to-get-values-in-dropdownlist-bounded-within-a-gridview

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
