WinForm (C #) CheckedListBox bind data and get the selected value (ValueMember) and display text (DisplayMember)

Source: Internet
Author: User

In this article I will discuss the issue of binding a data source to CheckedListBox space in WinForm development and getting the display text (displaymember) and corresponding actuals (ValueMember) for all selected elements in the control. The latter will be the focus of the discussion.

To make it easier to explain, first I want to preset some conditions.

Conditional presets:

1. A DataTable object mydatatable has been defined, and the fields and data for mydatatable are as follows:

Id Category name (TypeName)
1 Metal Products
2 General purpose and special mechanical equipment
3 Paper and paper products
4 Transportation and Transportation equipment
5 Electrical Machinery and equipment
6 Communication equipment
7 Computers and other
8 Electronic equipment
9 Instrumentation and culture
10 Machinery for office use

2. There is a CheckedListBox control in WinForm, with ID: mycheckedlistbox; a text control with ID: DisplayText; two buttons: Gets the selected text (Id:gettext), gets the selected actual value ( Id:getvalue). As follows:

Specific implementation:

1, to CheckedListBox control Mycheckedlistbox binding Data source, this method is very simple, fixed program, online a search a lot, directly on the code

    1. This    . Mycheckedlistbox.datasource = mydatatable;
    2. This . Mycheckedlistbox.valuemember = "ID"    ;
    3. This . Mycheckedlistbox.displaymember = "TypeName"  ;

2. Gets the display text (displaymember) of all the selected elements in the CheckedListBox control Mycheckedlistbox.

  1. // <summary>   
  2. ///button (GetText) Click event: Get the selected text   
  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.checkeditem S.count; i++)
  10. {
  11. Checkedtext + = (String.IsNullOrEmpty (checkedtext)? ""  :  ","    ) + this. Mycheckedlistbox.getitemtext (this. mycheckedlistbox.items[i]);
  12. }
  13. this.    Displaytext.text = Checkedtext;
  14. }

3. Gets the actual value (ValueMember) corresponding to all the selected elements in the CheckedListBox control Mycheckedlistbox.

  1. // <summary>   
  2. ///button (GetValue) Click event: Get the actual value selected   
  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. }

this article Yicheng boy . sent to: http://blog.yotuo.net (reproduced please retain this information)
starting address:http://blog.yotuo.net/post/2010/04/Get_CheckedlistBox_ValueMember_DisplayMember.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.