winfrom 窗體控件實現二級聯動
阿新 • • 發佈:2018-12-22
內容 感謝 標記 init bob and uri play 用兩個
事件,而這個時候用戶並沒有選擇內容,其SelectedValue也不是對應字段的值。那麽時寫在SelectedIndexChanged中的處理代碼就會因為SelectedValue的內容不正確引發異常。
一般網上找到的方法是添加一個標記位,在綁定前設置為false,綁定完成後設置回true。
綁定到ComboBox void BindComboBox() { flag=false; ComboxBox1.ValueMember="ValueColumn"; ComboxBox1.DisplayMember="DisplayColumn"; ComboxBox1.DataSource=DataTable1; flag=true; }
事件處理 private void ComboxBox1_SelectedIndexChanged(object sender, EventArgs e) { if(flag) { //Do something } }
另外還有一種辦法,就是在綁定前,將SelectedIndexChanged的委托去掉,等綁定完成後,再添加事件委托。
兩種方法都可以,但是之間的優劣暫時沒去比較。感覺好像處理一下委托會好點。因為這種辦法真的減少了事件的激發次數。
不知道還有沒有其他解決方案呢?
另,貼上一段完整的代碼例子。這個例子是訪問SqlServer數據庫的AdventureWorks,通過ProductCategory和ProductSubCategory兩級目錄分類去查看Product表的內容。分別使用兩個ComboBox和DataGridView完成數據綁定。效果就是選擇之後會聯動改變相關內容。
二級選擇框聯動顯示數據 public partial class frmProduct : Form { DataSet DS = new DataSet(); String ConnectionString = "integrated security=true; database=AdventureWorks; server=localhost;"; public frmProduct() { InitializeComponent(); } private void frmProduct_Load(object sender, EventArgs e) { SqlDataAdapter da = new SqlDataAdapter("select ProductCategoryID,[Name] from Production.ProductCategory", ConnectionString) ; cbbCategories.SelectedIndexChanged -= new EventHandler(cbbCategories_SelectedIndexChanged); da.Fill(DS, "ProductCategory"); cbbCategories.DataSource = null; cbbCategories.ValueMember = "ProductCategoryID"; cbbCategories.DataSource = DS.Tables["ProductCategory"]; cbbCategories.SelectedIndexChanged += new EventHandler(cbbCategories_SelectedIndexChanged); cbbCategories.DisplayMember = "Name";//這句放在事件委托之後才會有聯動效果,下同 } private void cbbCategories_SelectedIndexChanged(object sender, EventArgs e) { SqlDataAdapter da = new SqlDataAdapter("select ProductSubCategoryID,[Name] from Production.ProductSubCategory where ProductCategoryID=" + cbbCategories.SelectedValue.ToString(), ConnectionString) ; if (DS.Tables["ProductSubCategory"] != null) { DS.Tables["ProductSubCategory"].Clear(); } da.Fill(DS, "ProductSubCategory"); cbbSubCategories.SelectedIndexChanged -= new EventHandler(cbbSubCategories_SelectedIndexChanged); cbbSubCategories.DataSource = null; cbbSubCategories.ValueMember = "ProductSubCategoryID"; cbbSubCategories.DataSource = DS.Tables["ProductSubCategory"]; cbbSubCategories.SelectedIndexChanged += new EventHandler(cbbSubCategories_SelectedIndexChanged); cbbSubCategories.DisplayMember = "Name"; } private void cbbSubCategories_SelectedIndexChanged(object sender, EventArgs e) { if (cbbSubCategories.SelectedIndex == -1) return; SqlDataAdapter da=new SqlDataAdapter("select * from Production.Product where ProductSubCategoryID=" + cbbSubCategories.SelectedValue.ToString(), ConnectionString); dgvProduct.DataSource = null; if (DS.Tables["Product"] != null) DS.Tables["Product"].Clear(); da.Fill(DS, "Product"); dgvProduct.DataSource = DS.Tables["Product"]; } }
參考:https://www.cnblogs.com/Bonizlee/archive/2011/05/24/2054942.html?tdsourcetag=s_pctim_aiomsg感謝樓主提供的方法
winfrom 窗體控件實現二級聯動