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
- This . Mycheckedlistbox.datasource = mydatatable;
- This . Mycheckedlistbox.valuemember = "ID" ;
- This . Mycheckedlistbox.displaymember = "TypeName" ;
2. Gets the display text (displaymember) of all the selected elements in the CheckedListBox control Mycheckedlistbox.
- // <summary>
- ///button (GetText) Click event: Get the selected text
- // </summary>
- /// <param name= "Sender" ></param>
- /// <param name= "E" ></param>
- Private void Gettext_click (object sender, EventArgs e)
- {
- string checkedtext = string. Empty;
- for (int i = 0; i < this. Mycheckedlistbox.checkeditem S.count; i++)
- {
- Checkedtext + = (String.IsNullOrEmpty (checkedtext)? "" : "," ) + this. Mycheckedlistbox.getitemtext (this. mycheckedlistbox.items[i]);
- }
- this. Displaytext.text = Checkedtext;
- }
3. Gets the actual value (ValueMember) corresponding to all the selected elements in the CheckedListBox control Mycheckedlistbox.
- // <summary>
- ///button (GetValue) Click event: Get the actual value selected
- // </summary>
- /// <param name= "Sender" ></param>
- /// <param name= "E" ></param>
- Private void Getvalue_click (object sender, EventArgs e)
- {
- string checkedtext = string. Empty;
- for (int i = 0; i < this. MyCheckedlistBox.Items.Count ; i++)
- {
- if (this. mycheckedlistbox.getitemchecked (i))
- {
- this. mycheckedlistbox.setselected (i, true);
- Checkedtext + = (String.IsNullOrEmpty (checkedtext)? "" : "," ) + this. myCheckedlistBox.SelectedValue.ToString ();
- }
- }
- this. Displaytext.text = Checkedtext;
- }
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