Click here to Skip to main content
15,898,723 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
respected sir,
i tried to find the solution of this problem on google but i cant find the solution of this problem.

problem:-
departments show in combobox, now if we select any department than all employees of that department will show.
thanking you.
Posted
Comments
Sergey Alexandrovich Kryukov 12-Jun-11 0:29am    
Tag it! WPF, Forms, ASP.NET?!
--SA

This type of pattern is called master-detail. This is one of many resources that should help you; http://www.asp.net/data-access/tutorials/master-detail-filtering-with-a-dropdownlist-cs[^]
 
Share this answer
 
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;
using System.Data;
using System.Data.SqlClient;
namespace Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string connStr = @"Data Source=YOUSEF-PC\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True";
        private void Form1_Load(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = connStr;
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select distinct city  from employees";
            SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            while (reader.Read())
            {
                lstDepartments.Items.Add(reader["city"].ToString());
            }
            conn.Close();
        }
        
        private void lstDepartments_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstDepartments.SelectedText!=null)
            {
                lstEmployees.Items.Clear();
                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = connStr;
                conn.Open();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "select (firstname +' '+lastname) as fullname from employees where dept=@dept";
                cmd.Parameters.AddWithValue("@dept", lstDepartments.SelectedText);
                SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                lstEmployees.BeginUpdate();
                while (reader.Read())
                {
                    lstEmployees.Items.Add(reader[0].ToString());
                }
                lstEmployees.EndUpdate();
                conn.Close();
            }
        }
    }
}
 
Share this answer
 
v2
Comments
[no name] 13-Jun-11 13:51pm    
Ever heard of stored procedures, data binding or using statements?
Hi,
One solution is using a GridView and change GridView data source based on selected dropdownlist value. You will get implementation details from this link below
http://forums.asp.net/t/1588209.aspx[^]
Hope this will help.
 
Share this answer
 
You cannot file the solution because your choice of ComboBox is not adequate to the structure you need to show. For such purpose the TreeView would serve the best.

Alternative solution would be two combo boxes (each of them or both could be also a ListBox): one of them represents the name of department, another one — items withing the department. In this case, change in selection of the first box should re-populate the second one.

—SA
 
Share this answer
 

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