程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#ʵÏÖ´øËÑË÷¹¦ÄܵÄComboBox

C#ʵÏÖ´øËÑË÷¹¦ÄܵÄComboBox

編輯:C#入門知識

C#ʵÏÖ´øËÑË÷¹¦ÄܵÄComboBox。本站提示廣大學習愛好者:(C#ʵÏÖ´øËÑË÷¹¦ÄܵÄComboBox)文章只能為提供參考,不一定能成為您想要的結果。以下是C#ʵÏÖ´øËÑË÷¹¦ÄܵÄComboBox正文


由於畢業後任務沒有對接到專業問題,招致四五年沒有碰過Winform順序了。忽然由於任務問題,為了方便自己,所以想自己寫寫小winform小軟件,用於自己運用。在運用ComboBox控件時,遇到了重新綁定賦值出問題的狀況。

錯誤代碼如下:

if (CustomerBLL.select().Rows.Count > 0)
{
cbTcid.Items.Clear();
cbTcid.DataSource = CustomerBLL.select();
cbTcid.ValueMember = "Cid";
cbTcid.DisplayMember = "Cpname";
}
else
{
return;
}

正常狀況下,關於數據重新賦值的或許綁定數據源的時分,為了避免數據呈現問題,都會先清空原來數據,所以就這樣寫了,但是沒有相當恰恰這樣寫就呈現問題了。 於是在網上找了一下。發現有人是這樣操作的。【如下】

網上查找辦法如下:

if (CustomerBLL.select().Rows.Count > 0)
{
// cbTcid.Items.Clear();
if (cbTcid.Items.Count > 0)
{
cbTcid.DataSource = null;
cbTcid.Items.Clear();
}
cbTcid.DataSource = CustomerBLL.select();
cbTcid.ValueMember = "Cid";
cbTcid.DisplayMember = "Cpname";
}
else
{
return;
}

但是最後我嘗試了一下,在下次綁定數據源的時分,不清空上次的數據,能否可以呢?於是成功了。

if (CustomerBLL.select().Rows.Count > 0)
{
/*cbTcid.Items.Clear();
if (cbTcid.Items.Count > 0)
{
cbTcid.DataSource = null;
cbTcid.Items.Clear();
}*/
cbTcid.DataSource = CustomerBLL.select();
cbTcid.ValueMember = "Cid";
cbTcid.DisplayMember = "Cpname";
}
else
{
return;
}

而且最次要的是,在綁定數據源之前的數據,也自動清空了。能否真是這樣呢,還是如何呢?難道說這個ComboBox控件在下次綁定時分回自動清空,其他控件呢?歡送大家討論,而且此辦法能否會呈現代碼不標准呢?由於我的是小數據,遇到大數據能否可行呢,希望大家說說自己的建議。

以上所述是給大家引見的C# ComboBox控件“設置 DataSource 屬性後無法修正項集合”的完滿處理辦法,希望對大家有所協助,假如大家有任何疑問請給我留言,會及時回復大家的。在此也十分感激大家對網站的支持!

FindTextBox(this); else t = false; } /// <summary> /// ËÑË÷±à¼­Îı¾¿ò£¬Ìí¼ÓÎı¾¸Ä±äʼþ /// </summary> /// <param name="obj"></param> private void FindTextBox(DependencyObject obj) { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++) { DependencyObject child = VisualTreeHelper.GetChild(obj, i); if (child!=null && child is TextBox) { //×¢²áÎı¾¸Ä±äʼþ (child as TextBox).TextChanged += EditComboBox_TextChanged; } else { FindTextBox(child); } } } /// <summary> /// Îı¾¸Ä±ä£¬¶¯Ì¬¿ØÖÆÏÂÀ­ÌõÊý¾ÝÔ´ /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void EditComboBox_TextChanged(object sender, TextChangedEventArgs e) { TextBox tb = sender as TextBox; if(tb.IsFocused) { this.IsDropDownOpen = true; if (editText == this.Text) return; editText = this.Text; SetList(editText); } } /// <summary> /// ×éºÏ¿ò¹Ø±Õ£¬Êý¾ÝÔ´»Ö¸´ /// </summary> /// <param name="e"></param> protected override void OnDropDownClosed(EventArgs e) { base.OnDropDownClosed(e); if (MyItemsSource == null) return; foreach (var item in MyItemsSource) { if (!bindingList.Contains(item)) bindingList.Add(item); } } /// <summary> /// ¹ýÂË·ûºÏÌõ¼þµÄÊý¾ÝÏÌí¼Óµ½Êý¾ÝÔ´ÏîÖÐ /// </summary> /// <param name="txt"></param> private void SetList(string txt) { try { string temp1 = ""; string temp2 = ""; if (MyItemsSource == null) return; foreach (var item in MyItemsSource) { temp1 = item.GetType().GetProperty(this.DisplayMemberPath).GetValue(item, null).ToString(); if (string.IsNullOrEmpty(this.SelectedValuePath)) { temp2 = ""; } else { temp2 = item.GetType().GetProperty(this.SelectedValuePath).GetValue(item, null).ToString(); } if(temp1.Contains(txt)||temp2.StartsWith(txt)) { if (!bindingList.Contains(item)) bindingList.Add(item); } else if (bindingList.Contains(item)) { bindingList.Remove(item); } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } }

µ÷Ó÷½·¨¾ÍÊǽ«Êý¾ÝÔ´°ó¶¨µ½MyItemsSourceÉÏ£¬Ê£ÏµľͺÍÔ­ÓеÄComboBoxÓ÷¨Ò»ÑùÁË¡£

<local:EditComboBox MyItemsSource="{Binding ProList,Mode=TwoWay}" SelectedItem="{Binding Selpro,Mode=TwoWay}" SelectedValuePath="Id" DisplayMemberPath="Name"/>

Ч¹ûÑÝʾ

ÒÔÉϾÍÊDZ¾ÎĵÄÈ«²¿ÄÚÈÝ£¬Ï£Íû¶Ô´ó¼ÒµÄѧϰÓÐËù°ïÖú£¬Ò²Ï£Íû´ó¼Ò¶à¶àÖ§³Ö½Å±¾Ö®¼Ò¡£

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved