部门显示在组合框中,然后显示员工信息.相关部门 [英] departments show in combobox,then show employee info. of the related department
本文介绍了部门显示在组合框中,然后显示员工信息.相关部门的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尊敬的先生,
我试图在Google上找到此问题的解决方案,但找不到该问题的解决方案.
问题:-
部门在组合框中显示,现在,如果我们选择任何部门,则该部门的所有员工都将显示.
谢谢.
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.
推荐答案
这种类型的模式称为主从模式.这是应该帮助您的众多资源之一; http://www.asp.net/data-access/tutorials/master-detail-filtering-with-a-dropdownlist-cs [
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[^]
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();
}
}
}
}
您好,
一种解决方案是使用GridView并根据所选的dropdownlist值更改GridView数据源.您将在下面的此链接中获得实施详细信息
http://forums.asp.net/t/1588209.aspx [
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.
这篇关于部门显示在组合框中,然后显示员工信息.相关部门的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文