Results 1 to 10 of 10

Thread: Sorry another DropdownList & Datagrid Problem - PLS HELP!!!!

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2002
    Location
    The Twilight Zone
    Posts
    295

    Sorry another DropdownList & Datagrid Problem - PLS HELP!!!!

    Hi,

    Please can somebody help me on this, my head really hurts!!!!!

    The problem is this, i have a datagrid which reads in an xml file. The datagrid works fine. I have a dropdownlist column which i want to be populated with value colour. I have this simple xml file:

    Code:
    <cars>
      <car id="1">
        <name>VW Golf</name>
        <colour>Green</colour>
      </car>
      <car id="2">
        <name>Fieta</name>
        <colour>White</colour>
      </car>
    </cars>
    I would like this DDlist to have only the value and text set to its associated car colour. I have tried using the simple container :

    Code:
    <EditItemTemplate>
    <asp:DropDownList id="ddl"  DataTextField='<%# DataBinder.Eval(Container, "DataItem.colour") %>' runat="server"></asp:DropDownList>
    </EditItemTemplate>
    I have tried so many different combinations to get this simple functionality to work but just can't do it. I can bind all the colour values from the xml file to the DDL but i just want the value associated to that car. So each row in the datagrid will have a different colour in the DDL. I guess most of you are thinking why not just use a textbox. I basically then want to add other colour items to the DDL depending on the colour that is in the DDL.

    Hope i make sense. I really would appreciate any help on this, pointers, suggestions. I have read through every page on the net but can't seem to get this to work!

  2. #2
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: Sorry another DropdownList & Datagrid Problem - PLS HELP!!!!

    You must bind the DataGrid and DropDownLists separately.

    I find if you use method that creates an ArrayList of all colours and then use this as the DataSource for the DropDownList it works well:

    Code:
    ArrayList ddlColour_Binder() {
        ArrayList _colours = New ArrayList();
        _colours.Add("Green");
        _colours.Add("White");
        _colours.TrimToSize();
    }
    Code:
    <EditItemTemplate>
        <asp:DropDownList id="ddlColour" DataSource='<%# ddlColour_Binder() %>' DataTextField="Value" DataValueField="Value" SelectedValue='<%# DataBinder.Eval(Container, "DataItem.colour") %>' runat="server"></asp:DropDownList>
    </EditItemTemplate>
    Let me know if you need anything else.

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2002
    Location
    The Twilight Zone
    Posts
    295

    Re: Sorry another DropdownList & Datagrid Problem - PLS HELP!!!!

    Hi, thanks for your reply. I have implemented your code suggestions but i get the following 2 errors?

    1. ddColour_Binder(): not all code paths return a value
    2. Could not find any attribute 'SelectedValue' of element 'DropDownList'

    I have posted by code below.

    test.aspx

    Code:
    <%@ Page language="c#" Codebehind="test.aspx.cs" AutoEventWireup="false" Inherits="test.test" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    	<HEAD>
    		<title>test</title>
    		<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
    		<meta content="C#" name="CODE_LANGUAGE">
    		<meta content="JavaScript" name="vs_defaultClientScript">
    		<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    	</HEAD>
    	<body MS_POSITIONING="GridLayout">
    		<form id="Form1" method="post" runat="server">
    			<asp:datagrid id="DataGrid1" style="Z-INDEX: 103; LEFT: 176px; POSITION: absolute; TOP: 32px"
    				runat="server" DataKeyField="id" OnCancelCommand="dg_Cancel" OnEditCommand="dg_Edit" Font-Names="Arial"
    				Font-Size="X-Small" CellPadding="4" BackColor="White" BorderWidth="1px" BorderStyle="None"
    				BorderColor="#3366CC" AutoGenerateColumns="False">
    				<FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>
    				<SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle>
    				<ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>
    				<HeaderStyle Font-Bold="True" ForeColor="#CCCCFF" BackColor="#003399"></HeaderStyle>
    				<Columns>
    					<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
    					<asp:BoundColumn DataField="name" HeaderText="Name"></asp:BoundColumn>
    					<asp:TemplateColumn HeaderText="Colour">
    						<ItemTemplate>
    							<%# DataBinder.Eval(Container, "DataItem.colour") %>
    						</ItemTemplate>
    						<EditItemTemplate>
    							<asp:DropDownList id="ddColour" DataSource='<%# ddlColour_Binder() %>' DataTextField="Value" DataValueField="Value" SelectedValue='<%# DataBinder.Eval(Container, "DataItem.colour") %>' runat="server">
    							</asp:DropDownList>
    						</EditItemTemplate>
    					</asp:TemplateColumn>
    				</Columns>
    				<PagerStyle HorizontalAlign="Left" ForeColor="#003399" BackColor="#99CCCC" Mode="NumericPages"></PagerStyle>
    			</asp:datagrid>
    			</form>
    	</body>
    </HTML>
    test.aspx.cs

    Code:
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    
    namespace test
    {
    	/// <summary>
    	/// Summary description for test.
    	/// </summary>
    	public class test : System.Web.UI.Page
    	{
    		protected System.Web.UI.WebControls.DropDownList DropDownList1;
    		protected System.Web.UI.WebControls.Label lblSelected;
    		protected System.Web.UI.WebControls.DataGrid DataGrid1;
    	
    		private void Page_Load(object sender, System.EventArgs e)
    		{
    			DataSet ds = new DataSet();
    			ds.ReadXml(Server.MapPath("cars.xml"));
    
    			DataGrid1.DataSource = ds;
    			DataGrid1.DataBind();
    			
    			
    
    		}
    
    	
    
    		public void BindData() 
    		{	
    			DataSet ds = new DataSet();
    			ds.ReadXml(Server.MapPath("cars.xml"));
    
    			DataGrid1.DataSource = ds;
    			DataGrid1.DataBind();
    		}
    
    		
    		public ArrayList ddlColour_Binder() 
    		{
    			ArrayList _colours = new ArrayList();
    
    			_colours.Add("Green");
    			_colours.Add("White");
    			_colours.TrimToSize();
    
    		}
    
    
    		public void dg_Edit(object s, DataGridCommandEventArgs e) 
    		{
    			DataGrid1.EditItemIndex  = e.Item.ItemIndex;
    			BindData();
    		}
    		
    		public void dg_Cancel(object s, DataGridCommandEventArgs e) 
    		{
    			DataGrid1.EditItemIndex  = -1;
    			BindData();
    		}
    
    
    
    		
    		#region Web Form Designer generated code
    		override protected void OnInit(EventArgs e)
    		{
    			//
    			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
    			//
    			InitializeComponent();
    			base.OnInit(e);
    		}
    		
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		private void InitializeComponent()
    		{    
    			this.Load += new System.EventHandler(this.Page_Load);
    
    		}
    		#endregion
    
    
    	}
    }

    Thanks again.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2002
    Location
    The Twilight Zone
    Posts
    295

    Re: Sorry another DropdownList & Datagrid Problem - PLS HELP!!!!

    Ok, i have managed to solve half the problem. I can populate my dropdownlist in the datagrid by passing the rowID to a function which uses a DataView to filter the records and displays the respective colour back to the datagrid.

    Code:
    <EditItemTemplate>
    <asp:DropDownList id="ddColour" runat="server"  DataSource='<%# getmydata(Convert.ToInt32(DataBinder.Eval(Container.DataItem, "id"))) %>' DataValueField="colour" DataTextField="colour">
    </asp:DropDownList>
    </EditItemTemplate>
    Code:
    public DataView getmydata(int ID)   
    		{
    			DataSet ds = new DataSet();
    			ds.ReadXml(Server.MapPath("cars.xml"));
    
    			DataView dv;
    			dv = ds.Tables["Car"].DefaultView;
    			dv.RowFilter = "id LIKE '" + ID + "'";
    						
    			return dv;
    		}
    }
    The final thing i need to do is this, based on the colour in the dropdownlist i then to add another two colours. So i basically i have 3 colours (Red, Blue, Green) If the Dropdownlist colour is Red then only have items Blue & Green need to be added. So basically three possible combinations. I think the best way will be to use an array to store the 3 colours and use maybe an if then else statement. But how do i add the 2 colours dynamically to the DropDownList in teh datagrid?

    I hope you understand what i'm trying to do, not sure if i am going about it the right way. I just thought that i could bind the Dropdownlist to an xml file consisting of all 3 colours but i would i have the colour initially selected that maches the id.

    Really hope you guys understand what i am rambling on about here.

  5. #5
    Member Nikhil Aggarwal's Avatar
    Join Date
    Jun 2005
    Location
    New Delhi, India
    Posts
    36

    Re: Sorry another DropdownList & Datagrid Problem - PLS HELP!!!!

    Correct me if I am wrong, but this is what you are trying to achieve:
    a) You want the color value for each row in your datagrid to be displayed in a dropdownlist.
    b) The dropdownlist should have only this value in its list.

    What you could do is something like this:
    a) Create a hidden col for the color text in the datagrid
    b) Create a template col for the color dropdownlist.
    c) In the itemdatabound event of the datagrid do something like this
    Code:
    private void dg_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
         if(e.Item.ItemIndex != -1)
         {
              dropdownlist ddlColor = (dropdownlist) e.Item.Cells[4].findcontrol("ddlColor");
              ddlColor.Items.Add(e.Item.Cells[3].Text);
         }
    }
    Presuming
    - index of the hidden col is 3 and that of the template col is 4
    - Name of the dropdownlist is ddlColor
    Nikhil Aggarwal

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2002
    Location
    The Twilight Zone
    Posts
    295

    Re: Sorry another DropdownList & Datagrid Problem - PLS HELP!!!!

    Hi, thanks for the repy but you have misunderstood what i am trying to do. My dropdownlist currently displays the respective colour value & text from each row.

    Now what i want to do is, if the colour of the dropdownlist text is blue (pulled from the xml row) then add 2 more dropdown items red and green. If the dropdownlist text is red then add green and blue etc etc.

    The easiest way is to populate each dropdownlist in each row with the 3 colours red,green, blue but how will i the initial colour selected?

    Hope i make more sense.

  7. #7
    Member Nikhil Aggarwal's Avatar
    Join Date
    Jun 2005
    Location
    New Delhi, India
    Posts
    36

    Re: Sorry another DropdownList & Datagrid Problem - PLS HELP!!!!

    Hi. Sorry about the delayed reply. In case you havent resolved your problem by now this might be helpful.
    Code:
    private void dg_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
         if(e.Item.ItemIndex != -1)
         {
              dropdownlist ddlColor = (dropdownlist) e.Item.Cells[4].findcontrol("ddlColor");
                //Populate the dropdownlist with all the available colors
                ...........
               //Select the color from the dropdown list
              ddlColorItems.FindByText((e.Item.Cells[3].Text)).Selected=true;
         }
    }
    In order to populate the dropdownlist you could create an array in the page load event of all the available colors and set the datasource of the dropdownlist to this array in the ItemDataBound event.
    Hope this helps.
    Nikhil Aggarwal

  8. #8
    New Member
    Join Date
    Aug 2005
    Location
    LA - CA
    Posts
    2

    Re: Sorry another DropdownList & Datagrid Problem - PLS HELP!!!!

    ModernThinker,
    Did you ever resolve issue #2 (Could not find any attribute 'SelectedValue' of element 'DropDownList'). I am experiencing this as well. It may be a bug of VS and not matter what coding you do, it will never work. FYI. Please advise. Thanks.

    Robert

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2002
    Location
    The Twilight Zone
    Posts
    295

    Re: Sorry another DropdownList & Datagrid Problem - PLS HELP!!!!

    hi n8ndrew,

    No i never found an attribute SelectedValue for a DropDownList control. I don't think this exists?

    What problems are you having with your DDL & datagrid?

  10. #10
    New Member
    Join Date
    Aug 2005
    Location
    LA - CA
    Posts
    2

    Re: Sorry another DropdownList & Datagrid Problem - PLS HELP!!!!

    I am noticing that all of my dropdown list boxes that have their "SelectedValue" attributes set dynamically at run time have stopped working (it used to work). Apparently my issue may by more along the lines of a corrupted installation rather than a lack of proper coding. I now notice that on my HTML tab in Visual Studio all of my SelectedValue attributes now have a red sqiggly line under them (indicating an error) still no compile errors, however, the mouse-over assistance now displays the message "Could not find any attribute 'SelectedValue' of element DropDownList. Most of this code is generated automatically for me via the "(Databindings)" property wizards. Is this a Visual Studio Bug? Anyone else have similar experiences? Please advise. Thanks.


    Sample Code (HTML view):
    <TR>

    <TD style="WIDTH: 97px; HEIGHT: 65px"><asp:label id="Label9" runat="server" Font-Bold="True">Category:</asp:label><asp:dropdownlist id="DropDownListCategory" runat="server" DataSource="<%# DsEvents1 %>" DataMember="qryEventCatLu" DataTextField="Value" DataValueField="ID" SelectedValue='<%# MNI(DataBinder.Eval(DsEvents1, "Tables[dbo_spEventDetail].DefaultView.[0].EventCategoryID")) %>'></asp:dropdownlist></TD>

    <TD style="WIDTH: 411px; HEIGHT: 65px"><asp:label id="Label8" runat="server" Font-Bold="True">Hosting Unit:</asp:label><asp:dropdownlist id="DropDownListOrg" runat="server" Width="176px" DataSource="<%# DsEvents1 %>" DataMember="qryUnitLuView" DataTextField="Value" DataValueField="ID" SelectedValue='<%# MNI(DataBinder.Eval(DsEvents1, "Tables[dbo_spEventDetail].DefaultView.[0].UnitID")) %>'></asp:dropdownlist></TD>

    <TD style="HEIGHT: 65px"><asp:label id="Label7" runat="server" Font-Bold="True">Event Contact:</asp:label><asp:dropdownlist id="DropDownListContact" runat="server" Width="152px"></asp:dropdownlist></TD>

    </TR>

    See Ref links in ASP.net
    http://forums.asp.net/1016862/ShowPost.aspx
    http://forums.asp.net/1017122/ShowPost.aspx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width