This is a migrated thread and some comments may be shown as answers.

Selecting Item in RadDropDownList

5 Answers 737 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Shruti
Top achievements
Rank 1
Shruti asked on 05 Dec 2010, 03:07 AM
hello,
        I am using 2 RadDropDownLists.

        Depending on Item selected in dropdownlist1 i have to populate other dropdownlist.


 my code is:

private void Schedule_Load(object sender, EventArgs e)
{
            ddlSelectRoomType.DataSource = roomtype.getRoomType();

            ddlSelectRoomType.DisplayMember = "RoomTypeName";

            ddlSelectRoomType.ValueMember = "RoomTypeID";

            ddlSelectRoomType.Text = "Select Room Type";
}

 private void ddlSelectRoomType_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
{
            //code to populate other dropdown depending on the value selected in 1st dropdown(ddlSelectRoomType)
}

   when i am trying to display the value of the selected item it is displaying the text of the selected Item and not the value which in this case should be RoomTypeID

Also, i am unable to select other Items in the dropdown list1(ddlSelectRoomType). only first Item is getting selected and text of ddlSelectRoomType- Select Room Type
is not getting displayed rather the 1st item in the dropdownlist is displayed.

how can i fix this?

5 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 05 Dec 2010, 03:09 PM
Hello,

I've put together a small example of binding one drop down depending on another for you below.
hope this helps
Richard

Form...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
  
namespace RadDropDownList_C
{
    public partial class Form1 : Form
    {
  
        BindingList<Room> rooms = new BindingList<Room>();
  
        public Form1()
        {
            InitializeComponent();
        }
  
        private void Form1_Load(object sender, EventArgs e)
        {
            this.radDropDownListRoomTypes.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDownList;
            this.radDropDownListAvailableRooms.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDownList;
  
            // room types
            BindingList<RoomType> roomTypes = new BindingList<RoomType>();
            roomTypes.Add(new RoomType(1, "Double"));
            roomTypes.Add(new RoomType(2, "Single"));
            roomTypes.Add(new RoomType(3, "Twin"));
            roomTypes.Add(new RoomType(4, "Penthouse"));
              
            // available rooms
            rooms.Add(new Room(1, "The double suite"));
            rooms.Add(new Room(1, "The other double suite"));
            rooms.Add(new Room(2, "The single suite"));
            rooms.Add(new Room(2, "The other single suite"));
            rooms.Add(new Room(3, "The twin suite"));
            rooms.Add(new Room(3, "The other twin suite"));
            rooms.Add(new Room(4, "The penthouse suite"));
            rooms.Add(new Room(4, "The other penthouse suite"));
  
            // bind room types
            this.radDropDownListRoomTypes.DisplayMember = "RoomTypeName";
            this.radDropDownListRoomTypes.ValueMember = "RoomTypeId";
            this.radDropDownListRoomTypes.DataSource = roomTypes;
  
            this.radDropDownListRoomTypes.SelectedValue = 1;
  
        }
  
        private void radDropDownListRoomTypes_SelectedValueChanged(object sender, EventArgs e)
        {
            this.radDropDownListAvailableRooms.DataSource = (from Room r in rooms where r.RoomTypeId == Convert.ToInt32(radDropDownListRoomTypes.SelectedValue) select new { r.RoomName });
            this.radDropDownListAvailableRooms.DisplayMember = "RoomName";
            this.radDropDownListAvailableRooms.ValueMember = "RoomName";
        }
    }
  
    public class RoomType
    {
        public RoomType(int roomTypeId, string roomTypeName)
        {
            _roomTypeId = roomTypeId;
            _roomTypeName = roomTypeName;
        }
        private int _roomTypeId;
  
        public int RoomTypeId
        {
            get { return _roomTypeId; }
            set { _roomTypeId = value; }
        }
        private string _roomTypeName;
  
        public string RoomTypeName
        {
            get { return _roomTypeName; }
            set { _roomTypeName = value; }
        }
    }
  
  
  
    public class Room
    {
        public Room(int roomTypeId, string roomName)
        {
            _roomTypeId = roomTypeId;
            _roomName = roomName;
        }
        private int _roomTypeId;
  
        public int RoomTypeId
        {
            get { return _roomTypeId; }
            set { _roomTypeId = value; }
        }
        private string _roomName;
  
        public string RoomName
        {
            get { return _roomName; }
            set { _roomName = value; }
        }
    }  
  
  
}

Designer File
namespace RadDropDownList_C
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components;
  
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
  
        #region Windows Form Designer generated code
  
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.radDropDownListRoomTypes = new Telerik.WinControls.UI.RadDropDownList();
            this.radDropDownListAvailableRooms = new Telerik.WinControls.UI.RadDropDownList();
            ((System.ComponentModel.ISupportInitialize)(this.radDropDownListRoomTypes)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.radDropDownListAvailableRooms)).BeginInit();
            this.SuspendLayout();
            // 
            // radDropDownListRoomTypes
            // 
            this.radDropDownListRoomTypes.Location = new System.Drawing.Point(26, 51);
            this.radDropDownListRoomTypes.Name = "radDropDownListRoomTypes";
            this.radDropDownListRoomTypes.Size = new System.Drawing.Size(164, 22);
            this.radDropDownListRoomTypes.TabIndex = 0;
            this.radDropDownListRoomTypes.SelectedValueChanged += new System.EventHandler(this.radDropDownListRoomTypes_SelectedValueChanged);
            // 
            // radDropDownListAvailableRooms
            // 
            this.radDropDownListAvailableRooms.Location = new System.Drawing.Point(213, 51);
            this.radDropDownListAvailableRooms.Name = "radDropDownListAvailableRooms";
            this.radDropDownListAvailableRooms.Size = new System.Drawing.Size(201, 22);
            this.radDropDownListAvailableRooms.TabIndex = 1;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(556, 114);
            this.Controls.Add(this.radDropDownListAvailableRooms);
            this.Controls.Add(this.radDropDownListRoomTypes);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.radDropDownListRoomTypes)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.radDropDownListAvailableRooms)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();
  
        }
  
        #endregion
  
        private Telerik.WinControls.UI.RadDropDownList radDropDownListRoomTypes;
        private Telerik.WinControls.UI.RadDropDownList radDropDownListAvailableRooms;
    }
}
0
Shruti
Top achievements
Rank 1
answered on 05 Dec 2010, 05:56 PM
hello,
       Thank you. 
       I was able to populate one dropdown list depending on an item selected in other dropdown list.

        i want to display the DropDownList for Room type as,

        Select Room Type
          Class Room
           Laboratory

Class Room, Laboratory is in my database. I have set the Text of the DropDownList as "Select Room Type" in properties of Drop Down List.

     my code is:

       this.ddlRoomType.DisplayMember = "Select Room Type";
       this.ddlRoomType.SelectedValue = 1;
       this.ddlRoomType.DisplayMember = "RoomTypeName";
        this.ddlRoomType.ValueMember = "RoomTypeID";
            
        this.ddlRoomType.DataSource = roomtype.getRoomType();

     when i am executing my code, Class room is selected in dropdown list at the form_load. I want Select Room Type to be selected in the drop
     down list.
     
      what other property i need to set?

           
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 05 Dec 2010, 06:30 PM
Hello,

In a windows forms application, it is not normal practice to have a "Please Select an Option" in the drop down lists. But, if you must have one, then you can use the example below. Just replace the body of the form load event that I gave you before.

this.radDropDownListRoomTypes.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDownList;
this.radDropDownListAvailableRooms.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDownList;
// room types
BindingList<RoomType> roomTypes = new BindingList<RoomType>();
roomTypes.Add(new RoomType(1, "Double"));
roomTypes.Add(new RoomType(2, "Single"));
roomTypes.Add(new RoomType(3, "Twin"));
roomTypes.Add(new RoomType(4, "Penthouse"));
  
// available rooms
rooms.Add(new Room(1, "The double suite"));
rooms.Add(new Room(1, "The other double suite"));
rooms.Add(new Room(2, "The single suite"));
rooms.Add(new Room(2, "The other single suite"));
rooms.Add(new Room(3, "The twin suite"));
rooms.Add(new Room(3, "The other twin suite"));
rooms.Add(new Room(4, "The penthouse suite"));
rooms.Add(new Room(4, "The other penthouse suite"));
// bind room types
this.radDropDownListRoomTypes.DisplayMember = "RoomTypeName";
this.radDropDownListRoomTypes.ValueMember = "RoomTypeId";
this.radDropDownListRoomTypes.DataSource = roomTypes;
this.radDropDownListRoomTypes.SelectedIndex = -1;
this.radDropDownListRoomTypes.Text = "Please Select A Room Type";

Hope that helps
richard
0
Shruti
Top achievements
Rank 1
answered on 05 Dec 2010, 08:26 PM
it worked..

Thanks a lot..
0
Richard Slade
Top achievements
Rank 2
answered on 05 Dec 2010, 10:18 PM
Glad that worked for you.
Richard
Tags
DropDownList
Asked by
Shruti
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Shruti
Top achievements
Rank 1
Share this question
or