1. 程式人生 > >C#用ComboBox控件實現省與市的聯動效果的方法

C#用ComboBox控件實現省與市的聯動效果的方法

cat 數據 就是 mode var aio 默認 tchar bottom

本文實例講述了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#程序設計有所幫助。

除聲明外,跑步客文章均為原創,轉載請以鏈接形式標明本文地址
C#用ComboBox控件實現省與市的聯動效果的方法

本文地址: http://www.paobuke.com/develop/c-develop/pbk23162.html






相關內容

技術分享圖片C# WinForm實現窗體上控件自由拖動功能示例技術分享圖片C#中TextBox實現輸入提示功能的方法技術分享圖片C#讀取系統字體顏色與大小的方法技術分享圖片理解C#中的枚舉(簡明易懂)
技術分享圖片C#使用oledb讀取excel表格內容到datatable的方法技術分享圖片C#使用foreach語句遍歷隊列(Queue)的方法技術分享圖片C#中dotnetcharting的用法實例詳解技術分享圖片C#實現自定義定時組件的方法

C#用ComboBox控件實現省與市的聯動效果的方法