Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have doubt, how to populate one dropdown list box details to other in C#.net
without SQL commands.

What I have tried:

I tried to generate but no use,
ddcar.Items.Add("Select Your Car");
ddcar.Items.Add("MARUTHI");
ddcar.Items.Add("FORD");

ddmodel.Items.Add("800");
ddmodel.Items.Add("OMNI");

how to populate
Posted
Comments
aarif moh shaikh 6-Feb-16 1:10am    
you can use collections, json etc. for it.
Anisuzzaman Sumon 6-Feb-16 2:53am    
Please elaborate your question by adding your need and more code .
Sermaraj 6-Feb-16 3:17am    
i need to cascade details from one drop down box to another
BillWoodruff 6-Feb-16 5:08am    
See this for an example of Linq code to organize you data in a way suitable for this:

http://www.codeproject.com/Answers/1076978/How-to-create-daily-attendance-report-in-login-tim

Are you referring to a Win Forms 'ComboBox with its 'DropDownStyle Property set to 'DropDownList ?

Are you wanting to populate the second DropDown by doing a database query every time the selection in the first DropDown changes, or, do you have all the data read and available to use ?

In any case you need to describe where your data is, and how you get it.

Friends Thanks.

Finally Got solution = but with SQL connection;
---------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Web.Configuration;


public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string str = WebConfigurationManager.ConnectionStrings["conn"].ConnectionString;
SqlConnection cn = new SqlConnection(str);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select carid from car";
cmd.Connection = cn;
SqlDataAdapter ada = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
ada.Fill(ds,"car");
DropDownList1.DataSource = ds.Tables[0].DefaultView;
DropDownList1.DataValueField = ds.Tables[0].Columns["carid"].ToString();
DropDownList1.DataBind();


}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string str = WebConfigurationManager.ConnectionStrings["conn"].ConnectionString;
SqlConnection cn = new SqlConnection(str);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select cardetail from car where carid='" + DropDownList1.SelectedItem.ToString() + "'";

cmd.Connection = cn;
cn.Open();
SqlDataReader dr;
dr = cmd.ExecuteReader();
if (dr.Read())
{
TextBox1.Text = dr[0].ToString();


}
dr.Close();
cn.Close();
 
Share this answer
 
Hi Sermaraj,

If I have understood your quersion clearly then you want to fill the first dropdown box using internal list collection (without using any database table and SQL command)
then you want second dropdown box to be filtered based on selection of first list box but that too should manage internally withing C# code (without using using SQL command)

so if this is true then here is your solution

C#
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 _26196_filtering_collections_in_c_sharp
{
    public partial class Form1 : Form
    {
        public static List<CarModel> lstCarModel = new List<CarModel>();
        public Form1()
        {
            InitializeComponent();

            lstCarModel.Add(new CarModel("MARUTHI", "800"));
            lstCarModel.Add(new CarModel("FORD", "FIGO"));

            this.comboBox1.DataSource = lstCarModel;
            this.comboBox1.DisplayMember = "Car";
            this.comboBox1.ValueMember = "Car";
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            List<CarModel> FilteredCarModel = lstCarModel;
            var filtered = from CarModel cm in lstCarModel
                           where cm.Car == comboBox1.Text.ToString()
                           select cm;
            this.comboBox2.Items.Clear();
    

            foreach (CarModel carmodel in filtered)
            {
                this.comboBox2.Items.Add(carmodel.Model.ToString());
            }
           
        }

        public class CarModel
        {
            string car;
            public string Car{get { return car; }}
            string model;
            public string Model{get { return model; }}
            public CarModel(string car, string model){this.car= car;this.model= model;}
            public override string ToString(){return string.Format("{0}: {1}", car, model);}
        }
    }
}
 
Share this answer
 
Comments
BillWoodruff 7-Feb-16 0:02am    
You are on the right track here, but note that in the "real world" the 'Car class would probably contain a list of 'Models. And, constructing the contents of the second combo-box by rebuilding the list each time is costly. Given a manageable size data set, pre-computing the groups of models for each possible choice in the first combobox makes sense ... and then using datasource/displaymember/valuemember is indicated.
C#
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Person p1 = new Person { Id = "A", Name = "Kayes", Age = 29, JoiningDate = DateTime.Parse("2010-06-06") };
        Person p2 = new Person { Id = "B", Name = "Gibbs", Age = 34, JoiningDate = DateTime.Parse("2008-04-23") };
        Person p3 = new Person { Id = "C", Name = "Steyn", Age = 28, JoiningDate = DateTime.Parse("2011-02-17") };

        List<person> persons = new List<person>();
        persons.Add(p1);
        persons.Add(p2);
        persons.Add(p3);

       ExampleDDL.DataTextField = "Name";
        ExampleDDL.DataValueField = "Id";

        ExampleDDL.DataSource = persons;
        ExampleDDL.DataBind();
    }
}

public class Person
{
    public string Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public DateTime JoiningDate { get; set; }
}
Here is an example. I believe it can also work in WPF or Windows Forms. FYI, ExampleDDL is the dropdown list defined on the web page.
 
Share this answer
 
v3
Comments
RDBurmon 6-Feb-16 22:10pm    
Great solution but it only answers half of what CP asked
Craig (the.Net) Burkett 6-Feb-16 23:44pm    
It was intended to be a solid foundation of how to get data into a dropdown list. I presumed if they'd had additional questions they'd of asked. I was having a hard time discerning what they wanted and it also prodded someone to demonstrate their own prowess to provide a more complete solution, didn't it?

It is amazing to me how this very simple method is even overlooked in populating the grid.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900