日期:2014-05-17  浏览次数:20480 次

ASP.NET 省份选择器的问题。好纠结啊,纠结一个晚上了
自己构造了一个类
C# code
        public class ProvinceItem
             {
                     public int Id { get; set; }
                     public string Name { get; set; }
                    
             }


因为省份、城市选择器中,省份的数据库promary有2列,一列是Id,一列是省份名Name
在form中new了一个ProvinceItem实例item
C# code
                ProvinceItem item = new ProvinceItem();
                            item.Id = dataReader.GetInt32(dataReader.GetOrdinal("proID"));
                            item.Name = dataReader.GetString(dataReader.GetOrdinal("proName"));
                            cmbPro.Items.Add(item.Name);

为了显示出省份名,最后一行Add的是item.Name
但是问题来了
在城市的数据库city中,
为了方便查找,city的外键是promary的proID项
所以比较的时候,下面这段代码出错
  ProvinceItem pitem = (ProvinceItem) cmbPro.SelectedItem;
因为item.Name不是object类,而是string类,怎么弄才能实现省份的选择呢

完整代码如下:
C# code

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();
            string connStr = ConfigurationManager.ConnectionStrings["DbConn"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "select * from promary";
                    using (SqlDataReader dataReader = cmd.ExecuteReader())
                    {
                        while (dataReader.Read())
                        {
                            ProvinceItem item = new ProvinceItem();
                            item.Id = dataReader.GetInt32(dataReader.GetOrdinal("proID"));
                            item.Name = dataReader.GetString(dataReader.GetOrdinal("proName"));
                            cmbPro.Items.Add(item);
                        }
                    }
                }
            }
        }
        public void cmbPro_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbCity.Items.Clear();
            ProvinceItem pitem = (ProvinceItem) cmbPro.SelectedItem;
            string connStr = ConfigurationManager.ConnectionStrings["DbConn"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "select * from city where proID=@proID";
                    cmd.Parameters.Add(new SqlParameter("proID",pitem.Id));
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            string cityName = reader.GetString(reader.GetOrdinal("cityName"));
                            cmbCity.Items.Add(cityName);
                        }
                    }
                }
            }
        }
            public class ProvinceItem
             {
                     public int Id { get; set; }
                     public string Name { get; set; }
                    
             }
    }

        
}