C#用ComboBox控件完成省與市的聯動後果的辦法。本站提示廣大學習愛好者:(C#用ComboBox控件完成省與市的聯動後果的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#用ComboBox控件完成省與市的聯動後果的辦法正文
本文實例講述了C#用ComboBox控件完成省與市的聯動後果的辦法。分享給年夜家供年夜家參考。詳細完成辦法以下:
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 proID=@id";
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);
}
}
}
}
}
privatevoid comboBox1_SelectedIndexChanged(object sender,EventArgse) //留意這裡是comboBox1不是comboBox2
{
try
{
Province tem = (Province) comboBox1.SelectedItem;
int id = tem.ProvinceID;
comboBox2.Items.Clear();
using(SqlConnection conn =newSqlConnection("Data source=凡斌-VAIO;Initialcatalog=DBPromary;integrated security=true"))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
//查詢city(市)內外的 proID與promary(省)內外proID雷同的一切數據。
cmd.CommandText = "select * from city where proID =@id";
cmd.Parameters.Add(new SqlParameter("id", id));
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
comboBox2.Items.Add(dr.GetString(dr.GetOrdinal("cityName")));
}
}
}
}
catch(Exception ex)
{
MessageBox.Show("毛病信息:" + ex.Message);
}
}
}
class Province
{
public string ProvinceName { get;set; }
public int ProvinceID { get;set; }
}
//class People
//{
// public string name { get; set; }
// public int age { get; set; }
//}
}
以下圖所示:
願望本文所述對年夜家的C#法式設計有所贊助。