Binds ListBox to a custom object.

Source: Internet
Author: User

Binds listbox to a custom object.
1. textmember and valuemember of listbox must be attributes

2. The bound object must inherit the ilist

3. Bind the helper to use this private currencymanager = null;

The binding method uses this currencymanager = (currencymanager) datagrid1.bindingcontext [al]; 4. Make sure to set the datasource attribute 5. After the object content changes, refresh currencymanager. refresh ();

 

 

Link to: http://support.microsoft.com/kb/316303/zh-cn

Step-by-step examples
Create a windows application project in visual c #. net or visual c #2005. By default, form1 is created.
Add the class to the project.
Replace class1.cs with the following code:
Public class guitar
{
Private string make;
Private string model;
Private short year;
 
Public guitar ()
 {
 }

Public guitar (string make, string model, short year)
 {
Make = make;
Model = model;
Year = year;
 }

Public string make
 {
Get
  {
Return make;
  }
Set
  {
Make = value;
  }
 }
 
Public string model
 {
Get
  {
Return model;
  }
Set
  {
Model = value;
  }
 }

Public short year
 {
Get
  {
Return year;
  }
Set
  {
Year = value;
  }
 }
}
Close the class1.cs code window and switch to the form designer.
Add a datagrid control to form1. Adjust the size to adapt to three rows and four columns.
Add the four button controls to form1 and arrange the buttons horizontally.
Next, change the button1 text attribute.
Change to the button2 text attribute in the previous section.
Change to the text attribute of the first button3.
The text attribute of button4 was last modified.
The following code is added to the form1 class:
Private arraylist al = new arraylist ();
Private currencymanager = null;
Switch to the form designer, right-click the form, and click Properties.
Click the event icon, and double-click the load event to add the form1_load event to your code.
Paste the following code into the form1_load event:
Al. add (new guitar ("gibson", "les paul", 1958 ));
Al. add (new guitar ("fender", "jazz bass", 1964 ));
Al. add (new guitar ("guild", "IBM bird", 1971 ));
    
Currencymanager = (currencymanager) datagrid1.bindingcontext [al];
 
Datagrid1.datasource = al;
To view the switch of the form designer.
Double-click $ Next and add the following code to the button#click event:
Currencymanager. position ++;
Double-click the previous step and add the following code to the button2_click event:
Currencymanager. position --;
Double-click the first one and add the following code to the button3_click event:
Currencymanager. position = 0;
Double-click the nearest node and add the following code to the button4_click event:
Currencymanager. position = al. count-1;
Generate and run the project.
Click the command button to move between rows in the datagrid control. Note whether you need to edit the value of this object.

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.