三个表中的下拉列表代码:
class DBconnection
{
public const string CONNECTIONSTRING = "server=.;database=mydb;uid=sa;pwd=5587725";
}
class cbProd
{
private string _Prod_Code;
public string Prod_Code
{
get { return _Prod_Code; }
set { _Prod_Code = value; }
}
private string _Prod_Name;
public string Prod_Name
{
get { return _Prod_Name; }
set { _Prod_Name = value; }
}
public cbProd(string cd,string na)
{
_Prod_Code = cd;
_Prod_Name = na;
}
public cbProd() { }
}
class cbProdDA
{
private SqlConnection _Conn;
private SqlCommand _Cmd;
private SqlDataReader _DR;
public cbProdDA()
{
_Conn = new SqlConnection(DBconnection.CONNECTIONSTRING);
_Cmd = _Conn.CreateCommand();
}
public List<cbProd> Select()
{
List<cbProd> list=new List<cbProd>();
_Cmd.CommandText = "select * from productor ";
try
{
_Conn.Open();
_DR = _Cmd.ExecuteReader();
while (_DR.Read())
{
cbProd data = new cbProd();
data.Prod_Name = _DR["Prod_Name"].ToString();
data.Prod_Code = _DR["Prod_Code"].ToString();
list.Add(data);
}
}
finally
{
_Conn.Close();
}
return list;
}
}
class cbBrand
{
private string _Brand_Code;
public string Brand_Code
{
get { return _Brand_Code; }
set { _Brand_Code = value; }
}
private string _Brand_Name;
public string Brand_Name
{
get { return _Brand_Name; }
set { _Brand_Name = value; }
}
private string _Prod_Code;
public string Prod_Code
{
get { return _Prod_Code; }
set { _Prod_Code = value; }
}
private string _Brand_Memo;
public string Brand_Memo
{
get { return _Brand_Memo; }
set { _Brand_Memo = value; }
}
public cbBrand(string cd, string na)
{
_Brand_Code = cd;
_Brand_Name = na;
}
public cbBrand() { }
}
class cbBrandDA
{
private SqlConnection _Conn;
private SqlCommand _Cmd;
private SqlDataReader _DR;
public cbBrandDA()
{
_Conn = new SqlConnection(DBconnection.CONNECTIONSTRING);
_Cmd = _Conn.CreateCommand();
}
public List<cbBrand> Select(string cbProd_code)
{
List<cbBrand> list = new List<cbBrand>();
_Cmd.CommandText = "select * from brand where [email protected]_code";
_Cmd.Parameters.Clear();
_Cmd.Parameters.AddWithValue("@prod_code",cbProd_code);
try
{
_Conn.Open();
_DR = _Cmd.ExecuteReader();
while (_DR.Read())
{
cbBrand data = new cbBrand();
data.Brand_Name = _DR["Brand_Name"].ToString();
data.Brand_Code = _DR["Brand_Code"].ToString();
data.Brand_Memo = _DR["Brand_Memo"].ToString();
data.Prod_Code = _DR["Brand_Code"].ToString();
list.Add(data);
}
}
finally
{
_Conn.Close();
}
return list;
}
}
class cbCar
{
private string _Code;
public string Code
{
get { return _Code; }
set { _Code = value; }
}
private string _Brand;
public string Brand
{
get { return _Brand; }
set { _Brand = value; }
}
private string _Name;
public string Name
{
get { return _Name; }
set { _Name = value; }
}
private DateTime _Time;
public DateTime Time
{
get { return _Time; }
set { _Time = value; }
}
private decimal _Oil;
public decimal Oil
{
get { return _Oil; }
set { _Oil = value; }
}
private int _Powers;
public int Powers
{
get { return _Powers; }
set { _Powers = value; }
}
private int _Exhaust;
public int Exhaust
{
get { return _Exhaust; }
set { _Exhaust = value; }
}
private decimal _Price;
public decimal Price
{
get { return _Price; }
set { _Price = value; }
}
private string _Pic;
public string Pic
{
get { return _Pic; }
set { _Pic = value; }
}
public cbCar(string cd, string na)
{
_Code = cd;
_Name = na;
}
public cbCar() { }
}
class cbCarDA
{
private SqlConnection _Conn;
private SqlCommand _Cmd;
private SqlDataReader _DR;
public cbCarDA()
{
_Conn = new SqlConnection(DBconnection.CONNECTIONSTRING);
_Cmd = _Conn.CreateCommand();
}
public List<cbCar> Select(string brand_code)
{
List<cbCar> list = new List<cbCar>();
_Cmd.CommandText = "select * from Car where [email protected]_code";
_Cmd.Parameters.Clear();
_Cmd.Parameters.AddWithValue("@brand_code",brand_code);
try
{
_Conn.Open();
_DR = _Cmd.ExecuteReader();
while (_DR.Read())
{
cbCar data = new cbCar();
data.Code = _DR["Code"].ToString();
data.Name = _DR["Name"].ToString();
data.Brand = _DR["Brand"].ToString();
data.Time = Convert.ToDateTime(_DR["Time"]);
data.Oil = Convert.ToDecimal(_DR["Oil"]);
data.Powers = Convert.ToInt32(_DR["Powers"]);
data.Exhaust = Convert.ToInt32(_DR["Exhaust"]);
data.Price = Convert.ToDecimal(_DR["Price"]);
data.Pic = _DR["Pic"].ToString();
list.Add(data);
}
}
finally
{
_Conn.Close();
}
return list;
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void FillcbProd()
{
//查数据
List<cbProd> list = new cbProdDA().Select();
list.Insert(0,new cbProd("-1","==请选择=="));
//填进去
Productor.DataSource = list;//将集合list 赋值给数据源
Productor.DisplayMember = "Prod_Name";
Productor.ValueMember="Prod_Code";
}
private void FillcbBrand()
{
//获取当前选项(控件.SelectedItem)的code值
string prod_code=(Productor.SelectedItem as cbProd).Prod_Code;
List<cbBrand> list = new cbBrandDA().Select(prod_code);
list.Insert(0, new cbBrand("-1", "==请选择=="));
Brand.DataSource = list;
Brand.DisplayMember = "Brand_Name";
Brand.ValueMember = "Brand_Code";
}
private void FillcbCar()
{
string brand_code = (Brand.SelectedItem as cbBrand).Brand_Code;
List<cbCar> list = new cbCarDA().Select(brand_code);
list.Insert(0, new cbCar("-1", "==请选择=="));
Car.DataSource = list;
Car.DisplayMember = "Name";
Car.ValueMember = "Code";
}
private void Form1_Load(object sender, EventArgs e)
{
FillcbProd();
FillcbBrand();
FillcbCar();
}
private void Productor_SelectedIndexChanged(object sender, EventArgs e)
{
FillcbBrand();
}
private void Brand_SelectedIndexChanged(object sender, EventArgs e)
{
FillcbCar();
}
}