Wrox Programmer Forums
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old March 19th, 2006, 07:07 AM
Authorized User
Join Date: Jan 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default DropDownList Inside a datagrid

 i have this problem where i have to change the selected value in a dropdownlist which is inside a datagrid based on the data in the database(using Sql server).....I am bein able to bind data to the dropdownlist but am not bein able to get the selected value...
the problem is that if i set DropDownListName.selectedvalue=x;
then this value is being selected for the dropdownlists in all the rows of the datagrid.....this is what i have done...

private void bindDataToGrid(DataTable datatable)
ProjectDetails projectdetail=newProjectDetails();
datatable=projectdetail.getEmployeeProjectDetails( empId);
foreach(DataGridItem dgItem in grdProjectDetails.Items)
cboProject=(DropDownList)dgItem.FindControl("cboPr ojects");
cboProject.DataSource=datatable ;
cboProject.DataTextField= "projectName";
cboProject.DataValueField= "projectId";
DataRow rows=new DataRow();
for(int i=0;i<datatable.Rows.count;i++)
 cboProject.SelectedValue=rows[7];//As rows[7] contains the value to be selected

This is not working as the values keep getting overridden and i simply could not think of any other way..... So please could u help me.....

Old March 19th, 2006, 11:13 PM
Wrox Author
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons

Hello achhetri,
   I have done something similar to this, but not at runtime like this. Most of the time I use datagrids I am using it to display data and, if the user has the necessary permission, allow them to see my Edit/Update column of the datagrid.

In my <columns> collection of the datagrid I do something like this:

    <asp:dropdownlist id="drpLst" runat="server" datasource='<%# [datatable] %>' Datatextfield=<%# [textfield] %> datavaluefield = <%# [value] %> selectedvalue = <%# [value] %> />

Note that the datatable and the value for the selected value must be declared Public at class level and then on the method that responds to the datagrid Edit Command populate the datatable and the variable that holds the selected value.

Hope this helps.

"The one language all programmers understand is profanity."
Old March 20th, 2006, 12:27 AM
Friend of Wrox
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts

What I do in these siutations is to pull the value you want to set in the dropdownlist into another column in the datagrid. I set that columns visible property to false. Then, in the ItemDataBound event of the grid, I get a refernce to the ddl and to the columns value I want to set. Then I use a ddl.FindByValue(valuetoset), to set the ddl's value.


Old January 29th, 2007, 07:24 PM
Registered User
Join Date: Jan 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts

 i have a similar problem to Acchetri.I dont know if I will be able to explain this clearly. Anywayz here I go. I am having 3 dropdown lists inside a datagrid , am using VS.NET 2005. I have defined 2 SQL datasources for the first 2 drop down lists and they are populated on page load. The 3rd drop down list is also associated with a SQl data source where the query expects an input parameter which is nothing but the 2nd drop down list value.

I have this set up by calling the method to pass the SQL Data source Select parameter on the 2nd dropdown list's selectedindexchanged method. But when the 2nd drop down list changes, all the rows of the 3rd drop down changes to the same values. ie., the select parameter that I am passing to populate the 3rd dropdown applies across all the rows. So if I change a value on the 2nd row drop down , then both the first row and second row changes

datagrid definition

<asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False" PageSize="5" AllowPaging="True" OnItemDataBound="DataGrid1_ItemDataBound" OnPageIndexChanged="DataGrid1_PageIndexChanged" OnItemCommand="DataGrid1_ItemCommand">
                            <asp:TemplateColumn HeaderText="Sales Org">
                                    <asp:dropdownlist id="ddlSalesOrg" runat="server" DataSourceID="SqlDataSourceSalesOrg" DataTextField="salesorg_desc" DataValueField="salesorg_code"></asp:dropdownlist>
                                    <asp:RequiredFieldValidator ID="vldSalesOrg" runat="server" ControlToValidate="ddlSalesOrg"
                                        ErrorMessage="Sales Org cannot be blank">!</asp:RequiredFieldValidator>
                            <asp:TemplateColumn HeaderText="District">
                                    <asp:dropdownlist id="ddlDistrict" runat="server" DataSourceID="SqlDataSourceDistrict" DataTextField="district_name" DataValueField="district_code" AutoPostBack="True" OnSelectedIndexChanged="ddlDistrict_SelectedIndexC hanged"></asp:dropdownlist>
                            <asp:TemplateColumn HeaderText="Sales Rep">
                                    <asp:dropdownlist id="ddlSalesRep" runat="server" DataSourceID="SqlDataSourceSalesRep" DataTextField="SalesRep_Name" DataValueField="SalesRep_Code"></asp:dropdownlist>
                                <FooterStyle HorizontalAlign="Right"></FooterStyle>
                            <asp:TemplateColumn HeaderText="Delete">
                                    <asp:LinkButton id="DeleteRow" runat="server">X</asp:LinkButton>
                        <PagerStyle NextPageText="Next" PrevPageText="Previous" HorizontalAlign="Center" Mode="NumericPages"></PagerStyle>

Code Behind

 protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
           dsRole.RoleRecordRow dataRow;
           dataRow = (dsRole.RoleRecordRow)(((DataRowView)e.Item.DataIt em).Row);
           DropDownList SalesOrg = (DropDownList)(e.Item.FindControl("ddlSalesOrg"));
           dataRow.SalesOrg = SalesOrg.SelectedItem.Value.Trim();
           DropDownList ddlDistrict = (DropDownList)e.Item.FindControl("ddlDistrict");
           dataRow.District = ddlDistrict.SelectedItem.Value.Trim();
           DropDownList ddlSalesRep = (DropDownList)e.Item.FindControl("ddlSalesRep");
           // check if the dropdown is empty, else show the value;
           if (ddlSalesRep != null && ddlSalesRep.Items.Count != 0 && ddlSalesRep.SelectedItem != null)
               dataRow.SalesRep = ddlSalesRep.SelectedItem.Value.Trim();


 protected void ddlDistrict_SelectedIndexChanged(object sender, EventArgs e)
       string District_code = "";

       foreach (DataGridItem gridRow in DataGrid1.Items)
           DropDownList ddlDistrict = (DropDownList)(gridRow.FindControl("ddlDistrict")) ;
           DropDownList ddlSalesRep = (DropDownList)(gridRow.FindControl("ddlSalesRep")) ;

           District_code = ((DropDownList)gridRow.Cells[0].Controls[1]).SelectedItem.Value;
           SqlDataSourceSalesRep.SelectParameters.Add("distri ct_code", District_code);


So on the first row,if the district ddl changes to say "a" then the salesrep ddl should show only "a" district items.

On the second row, if district ddl is set to "b" then the salesrep ddl should show only "b" district items. but the way I do, when I change the second row to "b", then both first and second row shows only the "b" items for the sales rep dropdown list.

I understand that the data_itembound should be used along with the dropdown even changed to achieve what I want, but I dont know how I would do that. Tried the hidden field but again the same issue.

Can anyone please help me? I am stuck and running out of options.

Similar Threads
Thread Thread Starter Forum Replies Last Post
dropdownlist inside datagrid myunus General .NET 11 October 29th, 2006 07:37 AM
DropDownList inside a GridView aidoco ASP.NET 2.0 Basics 0 October 13th, 2006 04:24 AM
catch event of dropdownlist inside datagrid alyeng2000 ASP.NET 1.0 and 1.1 Professional 3 August 24th, 2004 10:44 AM
dropdownlist inside datagrid shaileshmark General .NET 5 June 27th, 2004 05:18 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.