首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

ASP.NET 省份选择器的有关问题。好纠结啊纠结一个晚下了

2012-10-16 
ASP.NET 省份选择器的问题。好纠结啊,纠结一个晚上了自己构造了一个类C# codepublic class ProvinceItem{pu

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; }                                 }    }        }


------解决方案--------------------


public class USState
{
private string myShortName;
private string myLongName;

public USState(string strLongName, string strShortName)
{

this.myShortName = strShortName;
this.myLongName = strLongName;
}

public string ShortName
{
get
{
return myShortName;
}
}

public string LongName
{

get
{
return myLongName;
}
}

public override string ToString()
{
return this.ShortName + " - " + this.LongName;
}
}


private void Form1_Load(object sender, EventArgs e)
{
ArrayList USStates = new ArrayList();
USStates.Add(new USState("Alabama", "AL"));
USStates.Add(new USState("Washington", "WA"));
USStates.Add(new USState("West Virginia", "WV"));
USStates.Add(new USState("Wisconsin", "WI"));
USStates.Add(new USState("Wyoming", "WY"));

this.comboBox1.DataSource = USStates;
this.comboBox1.DisplayMember = "LongName";
this.comboBox1.ValueMember = "ShortName";

}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{


}

DisplayMember 和ValueMember 表示要显示的和值

热点排行