Province tem = (Province)comboBox1.SelectedItem; // 因为在comb

时间:2022-06-21 08:02:57

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.SqlClient;

using System.Configuration;

namespace 省市联动

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

Tsql.GetConnection();

using (SqlConnection conn = new SqlConnection(Tsql.GetConnection()))

{

conn.Open();

using (SqlCommand cmd = conn.CreateCommand())

{

cmd.CommandText = “select * from promary”;

using (SqlDataReader dr = cmd.ExecuteReader())

{

while (dr.Read())

{

Province pr = new Province();

pr.proID = dr.GetInt32(dr.GetOrdinal(“proID”));  //这里要出格注意数据库字段的类型 proID在数据库里是int型

pr.proName = dr.GetString(dr.GetOrdinal(“proName”));

comboBox1.Items.Add(pr);//pr是一个Province东西。Province有一个proName和proID属性。我将一个东西填充到comboBox1里。再让comboBox1的DisplayMember属性设置proName。【也就是让comboBox1显示pr东西的proName属性】。这样在填充市级都市的时候我就可以找到当前选择项comboBox1.SelectedItem(或者当前选择东西)所对应的proID

}

}

}

comboBox1.SelectedIndex = 0;  //将comcomboBox1的默认选项设为0

}

}

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)

{

}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

{

comboBox2.Items.Clear(); //在选择的时候先清空一下combox1里面的所以项,,以免在改换省名的时候,与先前对应的省名下的市没有删除。

Province tem = (Province)comboBox1.SelectedItem; // 因为在comboBox1.Items.Add(pr)里添加的是东西。所以在这里讲comBox1所选中确当前东西赋给tem东西 (tem东西下面有两个属性 proName和proID)

int id = tem.proID;  //将当前选中东西的的proID赋值给id,给下面的sql盘问语句使用

Tsql.GetConnection();// 获取数据库连接字符串。

using (SqlConnection conn = new SqlConnection(Tsql.GetConnection()))

{

conn.Open();

using (SqlCommand cmd = conn.CreateCommand())

{

cmd.CommandText = “select * from city where [email protected]”;

cmd.Parameters.Add(new SqlParameter(“id”, id));

using (SqlDataReader dr = cmd.ExecuteReader())

{

while (dr.Read())

{

comboBox2.Items.Add(dr.GetString(dr.GetOrdinal(“cityName”)));

}

}

}

comboBox2.SelectedIndex = 0; //将comcomboBox1的默认选项设为0

}

}

}

class Tsql

{

public static string GetConnection() //数据库连接字符串

{

string getConn = ConfigurationManager.ConnectionStrings[“getConn”].ConnectionString;

return getConn;

}

}

class Province

{

public string proName { get; set; }

public int proID { get; set; }

}

}


代码如下:

using System.Data.SqlClient;

namespace 省市选择2

{

public partial class Form1 : Form

{

publicForm1()

{

InitializeComponent();

}

privatevoid Form1_Load(objectsender,EventArgs e)

{

/*

//测试代码:执行这段代码的情况下是可以吧 tom这个值添加到 comboBox1中的。

People p1 = new People();

p1.name = “tom”;

p1.age = 25;

comboBox1.Items.Add(p1.name);

*/

using(SqlConnection conn =newSqlConnection(“Data source=凡斌-VAIO;Initialcatalog=DBPromary;integrated security=true”))

{

conn.Open();

using(SqlCommand cmd = conn.CreateCommand())

{

cmd.CommandText = “select * from promary”;

using(SqlDataReader reader = cmd.ExecuteReader())

{

while (reader.Read())

{

//string str =reader.GetString(reader.GetOrdinal(“proName”));

//comboBox1.Items.Add(str);

Province  pv =new  Province();

//将comboBox1控件的DisplayMember属性设置为“ProvinceName”用来在comboBox1控件中显示ProvinceName属性的值

comboBox1.DisplayMember = “ProvinceName”;

pv.ProvinceName =reader.GetString(reader.GetOrdinal(“proName”));

pv.ProvinceID =reader.GetInt32(reader.GetOrdinal(“proID”));

//虽然pv属于东西,把一个东西添加到ComboxBox1中,控件显示的值为东西的类名,但是在前面comboBox1.DisplayMember =”ProvinceName”处已经设置好控件要显示的为 Province类的ProvinceName值,所以添加进去的是pv东西的ProvinceName值

comboBox1.Items.Add(pv);

}

}

}

}

}