469,927 Members | 1,469 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

DropdownList in Gridview won't select the correct value

Hi there,
I have a Gridview control which has a dropdownlist in every row. The dropdowns are populated dynamically from a database (so the content is different in each row. There is also a select button for each row so that the the user can choose a row and the values will be added to a shopping cart.

My problem is that the page loads fine and everything is showing as it should be. However, when I select a row the dropdownlist for that row always selects the first item in the list (regardless of the option I selected). This is really frustrating because it took so long to get the page to bind, I really thought this would be the easy part. Here's the code so you can see where I'm at so far:

<asp:GridView ID="Grid" runat="server"
OnSelectedIndexChanged="Grid_SelectedIndexChanged" >


<asp:TemplateField HeaderText="ID">

<asp:Label ID="Label1" runat="server" Text='<%# Bind("SubProdID") %>'>

<asp:TemplateField HeaderText="Name" >

<asp:Label ID="Label2" runat="server" Text='<%# Bind("SubProdName") %>'>

<asp:TemplateField HeaderText="Desc">
<asp:Label ID="Label3" runat="server" Text='<%# Bind("SubProdDesc") %>'>

<asp:TemplateField HeaderText="Options" >
<asp:DropDownList ID="DDL" runat="server" Width="60">

HtmlEncode="False" />

Text="Add to cart">
<ControlStyle CssClass="SmallButtonText" />


<asp:Label ID="LblId" runat="server"></asp:Label>
<br />
<asp:Label ID="LblName" runat="server"></asp:Label>
<br />
<asp:Label ID="LblDesc" runat="server"></asp:Label>
<br />
<asp:Label ID="LblOption" runat="server"></asp:Label>
<br />
<asp:Label ID="LblPrice" runat="server"></asp:Label>

*****code behind*******

public partial class GetSubProductTest2 : System.Web.UI.Page

string productId = "35";

List<SubProductWrapper> SPWlist = new List<SubProductWrapper>();

protected void Page_Load(object sender, EventArgs e)

List<SubProductWrapper> SubProduct = SubProductAccess.GetSubProducts(productId);

SPWlist = SubProduct;
Grid.DataSource = SPWlist;


protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)

if (e.Row.RowType == DataControlRowType.DataRow)

string SubProdID1 = Grid.DataKeys[e.Row.DataItemIndex].Value.ToString();

int SubProdID2 = Int32.Parse(SubProdID1);

foreach (SubProductWrapper SubProd in SPWlist)

List<SubProductOptionsWrapper> SubProdOptions = (SubProd.SubProdOptions);

foreach (SubProductOptionsWrapper options in SubProdOptions)

int SubProdID = options.SubProdID;
int OptionID = options.OptionID;
string OptionName = options.OptionName;

DropDownList ddl = (DropDownList)e.Row.FindControl("DDL");

ddl.DataSource = SubProdOptions;
ddl.DataTextField = "OptionName";
ddl.DataValueField = "OptionID";
ddl.SelectedValue = OptionName;

if (SubProdID2 == SubProdID)
ddl.Items.Add(new ListItem(OptionName));



protected void Grid_SelectedIndexChanged(object sender, EventArgs e)
GridViewRow row = Grid.SelectedRow;

int id = (int)Grid.SelectedDataKey.Values["SubProdID"];
string name = Grid.SelectedRow.Cells[1].Text;
string desc = Grid.SelectedRow.Cells[2].Text;

DropDownList dr = ((DropDownList)row.FindControl("DDL"));
int index = dr.SelectedIndex;
string option = dr.Items[index].Value;

string price = Grid.SelectedRow.Cells[4].Text;

LblId.Text = id.ToString();
LblName.Text = name;
LblDesc.Text = desc;
LblOption.Text = option;
LblPrice.Text = price;

Dec 30 '07 #1
0 1592

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by P. Yanzick | last post: by
1 post views Thread by mitchman10 | last post: by
3 posts views Thread by Jon Paal | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.