[Original] C # implement list binding

Source: Internet
Author: User

Brief description: I sometimes encounter such problems during development, such as: I have such a notebook brand table

CATEGORY ID

Category name

1001

Acer

1002

Lenovo

1003

ASUS

1004

HP

Now I need to display these products to users to make some or more choices. After a user makes a choice, all I need is to select the product ID corresponding to the product, the customer does not know the selected product ID. In terms of web development, it seems that many list controls have provided good support, such as checkboxlist, radiobuttonlist, dropdownlist, and other Web controls all provide two attributes (Value and text ), value can be used to store product IDs, text can be used to store product names, and displayed to users. However, in winform development, such list controls are not as convenient as those in the Web, such as ComboBox and checkedlistbox list controls. They only provide the ability to bind an object. What should I do if I need to implement a function similar to Web? The description is a bit depressing, and the language expression capability needs to be greatly enhanced. The following is an example:

    1. First, define a notebook brand structure to store the notebook brand ID and brand name.

/// <Summary>

/// Notebook type

/// </Summary>

/// <Typeparam> Value Type </typeparam>

/// <Typeparam> text value type </typeparam>

Public struct computertype <t, k>

{

Private t m_value;

/// <Summary>

/// Value

/// </Summary>

Public T value

{

Get {return m_value ;}

Set {m_value = value ;}

}

 

Private K m_text;

/// <Summary>

/// Text Value

/// </Summary>

Public K text

{

Get {return m_text ;}

Set {m_text = value ;}

}

 

Public override string tostring ()

{

Return m_text.tostring ();

}

}

Note: You need to override this tostring ()

2. bind it to the brand table,CodeAs follows:

Computertype <int, string> computertype

Foreach (string strtype in typearray)

{

Computertype = new packettype <int, string> ();

Computertype. value = "brand name ";

Computertype. Text = "brand ID"

Combobox1.items. Add (computertype );

}

PS: it becomes very flexible to use the bound object here.

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.