By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,194 Members | 1,657 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,194 IT Pros & Developers. It's quick & easy.

DropdownList in Gridview won't select the correct value

P: 1
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"
AutoGenerateColumns="False"
CssClass="Grid"
OnRowDataBound="Grid_RowDataBound"
DataKeyNames="SubProdID"
OnSelectedIndexChanged="Grid_SelectedIndexChanged" >

<Columns>

<asp:TemplateField HeaderText="ID">

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



<asp:TemplateField HeaderText="Name" >

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


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

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

<asp:BoundField
HeaderText="Price"
DataField="SubProdPrice"
DataFormatString="{0:C}"
ReadOnly="True"
HtmlEncode="False" />

<asp:ButtonField
ButtonType="Button"
CommandName="Select"
Text="Add to cart">
<ControlStyle CssClass="SmallButtonText" />
</asp:ButtonField>

</Columns>
</asp:GridView>


<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;
Grid.DataBind();

}



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
Share this question for a faster answer!
Share on Google+

Post your reply

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