求助,C#中关于ComboBox.SelectedIndex的问题
的有关信息介绍如下:你只需将该方法private void Form1_Load(object sender, EventArgs e)体中的内容单独用一个方法来写,比如private void BindProvince(){ /*这里放的是判塌你窗体加载时的方法体*/}然后再在已经被你切掉的Form1_Load中加上BindProvince();最后的效果大致如此:public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { BindProvince(); //调用绑定枣冲蔽的方法 } private void BindProvince() { SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=E:\\系统开发\\diquguanli\\diqu.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True;"); SqlDataAdapter da = new SqlDataAdapter("select PID, Pname from Province", con); DataSet ds = new DataSet(); da.Fill(ds, " Province "); DataTable dtGroup = ds.Tables["Province "]; comboBox1.DataSource = dtGroup; comboBox1.DisplayMember = " PName"; comboBox1.ValueMember = " PID"; } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { if (comboBox1.SelectedIndex > -1) { DataRowView drv = (DataRowView)comboBox1.SelectedItem; string pId = drv.Row["PID"].ToString(); SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=E:\\系统开发\\diquguanli\\diqu.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True;"); SqlDataAdapter da = new SqlDataAdapter("select CID, CName from City where PID='" + pId + "'", con); DataSet ds = new DataSet(); da.Fill(ds, "City"); DataTable dtUser = ds.Tables["City"]; comboBox2.DataSource = dtUser; comboBox2.DisplayMember = "CName"凳州; comboBox2.ValueMember = "CID"; } } }试试看吧!