Weird checkedlistbox Data Binding

Source: Internet
Author: User

Today, checkedlistbox is used to summarize common knowledge.

1. Add item: items. Add

 
Checkedlistbox1.items. Add ("June");
Checkedlistbox1.items. Add ("Jun");

2. Determine whether getitemchecked (INDEX) is selected for item I)

 
Checkedlistbox1.getitemchecked (I)

3. Set whether setitemchecked (index, bool) or setitemcheckstate (index, checkstate) is selected for item I)

 
Checkedlistbox1.setitemchecked (index,True);
Checkedlistbox1.setitemcheckstate (index, checkstate. Unchecked );

4. Set all options

For(IntI =0; I <listboxled. Items. Count; I ++)
{
Checkedlistbox1.setitemcheckstate (I, checkstate. Checked );
//Checkedlistbox1.setitemchecked (I, true );
}

5. Data Binding

Checkedlistbox should be extended by ListBox, but in use, you may find that -- it does not support the datasource attribute and cannot specify its data source as a datatable as ListBox.

As a matter of fact, checkedlistbox has the datasource attribute like ListBox, and both displaymember and valuememeber attributes are available, but intelliisense cannot intelligently perceive them.

Therefore, we canCodeBind the checkedlistbox.

Dataset DS = BLL. getallstudent ();
Checkedlistbox1.datasource = Ds. Table [0];
Checkedlistbox1.valuemember ="Student_id";
Checkedlistbox1.displaymember ="Student_name";

6. How to obtain the displaymember and valuemember of the checkedlistbox1 selected item

Method 1:

For(IntI =0; I <checkedlistbox1.checkeditems. Count; I ++)
{
Datarowview DV = (datarowview) checkedlistbox1.checkeditems [I]);
StringId = DV ["Student_id"]. Tostring ();
StringName = DV ["Student_name"]. Tostring ();
}

Method 2:

Get Text: it is easy to get text. You can get it on the checkedlistbox. Get the text of the selected item.

 
StringName = checkedlistbox1.getitemtext (checkedlistbox1.items [I]);

Get the value of value: here we use a technique to obtain the value by binding it to the dataset of checkedlistbox. (It is shown on the Internet that the index obtained in checkedlistbox is the same as the corresponding value in dataset..I want to explain the cause.)

 
StringName = Ds. Tables [0]. Rows [I] ["Student_name"]. Tostring ();//Here I is the index of the selected checkedlistbox item

 

If you think that checkedlistbox is commonly used, please add it.

 

Related Article

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.