c#省市联动

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 DBHelper;
using MySql.Data.MySqlClient; namespace WindowsFormsApplication1
{ public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
//string sql = "select * from s_province";
////DataTableCollection dc = SqlHelper.GetTableText(sql, null);
//MySqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.connectionString, CommandType.Text, sql, null);
//while (dr.Read())
//{
// zi a = new zi(); // comboBox1.DisplayMember = "ProvinceName";
// comboBox1.ValueMember = "ProvinceID";
// a.provinceID = Convert.ToInt32(dr[0]);
// a.provinceName = Convert.ToString(dr[1]);
// comboBox1.Items.Add(a);
//}
//comboBox1.SelectedIndex = 0;
this.comboBox1.Items.Clear();
string sql = "select * from s_province";
DataSet ds = SqlHelper.ExecuteDataSetText(sql, null);
comboBox1.DisplayMember = "ProvinceName";
comboBox1.ValueMember = "ProvinceID";
comboBox1.DataSource = ds.Tables[0];
} private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
//this.comboBox3.Items.Clear();
//qu qx = (qu)comboBox2.SelectedItem;
//int id = qx.cityID;
//string sql = "select * from qu where quid=@id";
//MySqlParameter p = new MySqlParameter("@id", id);
//MySqlParameter[] pa = new MySqlParameter[] { p };
//MySqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.connectionString, CommandType.Text, sql, pa);
//while (dr.Read())
//{
// comboBox3.Items.Add(dr[1]);
//}
} private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
// this.comboBox2.Items.Clear();
//zi ax = (zi)comboBox1.SelectedItem; //int id = ax.provinceID; //string sql = "select * from s_city where ProvinceID=@id";
//MySqlParameter p = new MySqlParameter("@id", id);
//MySqlParameter[] pa = new MySqlParameter[] { p };
//MySqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.connectionString, CommandType.Text, sql, pa);
//while (dr.Read())
//{
// //qu q = new qu();
// //comboBox1.DisplayMember = "CityName";
// //comboBox1.ValueMember = "CityID";
// //q.cityID = Convert.ToInt32(dr[0]);
// //q.cityName = Convert.ToString(dr[1]);
// //comboBox2.Items.Add(q);
// comboBox2.Items.Add(dr[1]);
//}
string id = Convert.ToString(comboBox1.SelectedValue);
load_city(id); }
private void load_city(string provinceid)
{
string sql = "select * from s_city where ProvinceID="+provinceid;
DataSet ds = SqlHelper.ExecuteDataSetText(sql, null);
comboBox2.DisplayMember = "CityName";
comboBox2.ValueMember = "CityID";
comboBox2.DataSource = ds.Tables[0];
} private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{ }
}
class zi
{
public string provinceName;
public string ProvinceName
{
get { return provinceName; }
set { provinceName = value; }
}
public int provinceID;
public int ProvinceID
{
get { return provinceID; }
set { provinceID = value; }
}
} class qu
{
public string cityName;
public string CityName
{
get { return cityName; }
set { cityName = value; }
}
public int cityID;
public int CityID
{
get { return cityID; }
set { cityID = value; }
}
}
}

  

上一篇:Linux 学习笔记之超详细基础linux命令(the end)


下一篇:WordPress 实用SEO插件总结