当前位置:好百科>百科词条>求助,C#中关于ComboBox.SelectedIndex的问题

求助,C#中关于ComboBox.SelectedIndex的问题

2024-10-30 13:23:00 编辑:zane 浏览量:597

求助,C#中关于ComboBox.SelectedIndex的问题

的有关信息介绍如下:

求助,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"; } } }试试看吧!

版权声明:文章由 好百科 整理收集,来源于互联网或者用户投稿,如有侵权,请联系我们,我们会立即处理。如转载请保留本文链接:https://www.haobaik.com/tips/182619.html
热门文章