WinForm(C#)CheckedlistBox綁定資料,並獲得選中的值(ValueMember)和顯示文本(DisplayMember)

來源:互聯網
上載者:User

標籤:winform   style   c   class   blog   tar   

本文中我將和大家討論關於在WinForm開發中給CheckedlistBox空間綁定資料來源,並擷取控制項中選中的所有元素的顯示文本(DisplayMember)和對應的實際值(ValueMember)的問題,後者將是討論的重點。

為了更方便地說明,首先我要預設一些條件。

條件預設:

1、已定義一個DataTable對象myDataTable,並且myDataTable的欄位及資料如下:

ID 分類名稱(TypeName)
1 金屬製品
2 通用及專用機械裝置
3 紙及紙製品
4 交通運輸裝置
5 電氣機械及器材
6 通訊裝置
7 電腦及其他
8 電子裝置
9 儀器儀錶及文化
10 辦公用機械

2、WinForm狀體中有一個CheckedlistBox控制項,ID為:myCheckedlistBox;一個文本控制項,ID為:DisplayText;兩個按鈕:擷取已選的文本(ID:GetText),擷取已選的實際值(ID:GetValue)。如下:

 

具體實現:

1、給CheckedlistBox控制項myCheckedlistBox綁定資料來源,這個方法很簡單,固定程式,網上一搜一大把,就直接上代碼了

 
  1. this.myCheckedlistBox.DataSource = myDataTable;    
  2. this.myCheckedlistBox.ValueMember = "ID";    
  3. this.myCheckedlistBox.DisplayMember = "TypeName";  

2、擷取CheckedlistBox控制項myCheckedlistBox中已選中的所有元素的顯示文本(DisplayMember)。

 
  1. /// <summary>    
  2. /// 按鈕(GetText)單擊事件:擷取擷取已選的文本    
  3. /// </summary>    
  4. /// <param name="sender"></param>    
  5. /// <param name="e"></param>    
  6. private void GetText_Click(object sender, EventArgs e)    
  7. {    
  8.     string checkedText = string.Empty;    
  9.     for (int i = 0; i < this.myCheckedlistBox.CheckedItems.Count; i++)    
  10.     {    
  11.         checkedText += (String.IsNullOrEmpty(checkedText) ? "" : ",") + this.myCheckedlistBox.GetItemText(this.myCheckedlistBox.Items[i]);    
  12.     }    
  13.     this.DisplayText.Text = checkedText;    
  14. }   

3、擷取CheckedlistBox控制項myCheckedlistBox中已選中的所有元素對應的實際值(ValueMember)。

 
  1. /// <summary>    
  2. /// 按鈕(GetValue)單擊事件:擷取已選的實際值    
  3. /// </summary>    
  4. /// <param name="sender"></param>    
  5. /// <param name="e"></param>    
  6. private void GetValue_Click(object sender, EventArgs e)    
  7. {    
  8.     string checkedText = string.Empty;    
  9.     for (int i = 0; i < this.myCheckedlistBox.Items.Count; i++)    
  10.     {    
  11.         if (this.myCheckedlistBox.GetItemChecked(i))    
  12.         {    
  13.             this.myCheckedlistBox.SetSelected(i, true);    
  14.             checkedText += (String.IsNullOrEmpty(checkedText) ? "" : ",") + this.myCheckedlistBox.SelectedValue.ToString();    
  15.         }    
  16.     }    
  17.     this.DisplayText.Text = checkedText;    
  18. }   

 

 

本文宜城小子  發於:http://blog.yotuo.net(轉載請保留此資訊)
首發地址:http://blog.yotuo.net/post/2010/04/Get_CheckedlistBox_ValueMember_DisplayMember.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.