Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Professional
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old March 19th, 2006, 06:07 AM
Authorized User
 
Join Date: Jan 2006
Location: gangtok, Sikkim, India.
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default DropDownList Inside a datagrid

Hi,
 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)
{
grdProjectDetails.DataSource=datatable;
grdProjectDetails.DataBind();
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";
cboProject.DataBind();
}
DataRow rows=new DataRow();
for(int i=0;i<datatable.Rows.count;i++)
{
 rows=datatable.Rows[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.....

achhetri
__________________
achhetri
  #2 (permalink)  
Old March 19th, 2006, 10:13 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

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:

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

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."
  #3 (permalink)  
Old March 19th, 2006, 11:27 PM
Friend of Wrox
Points: 4,332, Level: 27
Points: 4,332, Level: 27 Points: 4,332, Level: 27 Points: 4,332, Level: 27
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2003
Location: , NJ, USA.
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

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.

Jim

  #4 (permalink)  
Old January 29th, 2007, 06:24 PM
Registered User
 
Join Date: Jan 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

 Hi,
 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">
                        <Columns>
                            <asp:TemplateColumn HeaderText="Sales Org">
                                <ItemTemplate>
                                    <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>
                                </ItemTemplate>
                            </asp:TemplateColumn>
                            <asp:TemplateColumn HeaderText="District">
                                <ItemTemplate>
                                    <asp:dropdownlist id="ddlDistrict" runat="server" DataSourceID="SqlDataSourceDistrict" DataTextField="district_name" DataValueField="district_code" AutoPostBack="True" OnSelectedIndexChanged="ddlDistrict_SelectedIndexC hanged"></asp:dropdownlist>
                                </ItemTemplate>
                            </asp:TemplateColumn>
                            <asp:TemplateColumn HeaderText="Sales Rep">
                                <ItemTemplate>
                                    <asp:dropdownlist id="ddlSalesRep" runat="server" DataSourceID="SqlDataSourceSalesRep" DataTextField="SalesRep_Name" DataValueField="SalesRep_Code"></asp:dropdownlist>
                                </ItemTemplate>
                                <FooterStyle HorizontalAlign="Right"></FooterStyle>
                                <FooterTemplate>
                                </FooterTemplate>
                            </asp:TemplateColumn>
                            <asp:TemplateColumn HeaderText="Delete">
                                <ItemTemplate>
                                    <asp:LinkButton id="DeleteRow" runat="server">X</asp:LinkButton>
                                </ItemTemplate>
                            </asp:TemplateColumn>
                        </Columns>
                        <PagerStyle NextPageText="Next" PrevPageText="Previous" HorizontalAlign="Center" Mode="NumericPages"></PagerStyle>
                    </asp:datagrid>


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

 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
dropdownlist inside datagrid myunus General .NET 11 October 29th, 2006 06: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



All times are GMT -4. The time now is 01:53 AM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.