ListBox Data Binding in winform. The value of ListBox is incorrect.

Source: Internet
Author: User

ListBox Data Binding in winform. The value of ListBox is incorrect.


ListBox Data Binding

Dataset DS = new dataset (); <br/> DS = sqlhelper. executedataset (syshelp. connstr, commandtype. storedprocedure, "proc_selname"); <br/> datatable dt = Ds. tables [0]; <br/> listbox1.datasource = DT; <br/> listbox1.displaymember = DT. columns [0]. columnname; <br/> listbox1.valuemember = DT. columns [0]. columnname;

Note: 1. sqlhelper is the help class for database operations. It executes the stored procedure and obtains dataset.

2. displaymember indicates the data displayed in ListBox.

3. valuemember is the data hidden in ListBox.


Value of ListBox

After datasource is bound, the value of listbox1.selecteditem cannot be used. You must convert the value or use the value of listbox1.selectedvalue.

Datarowview DRV = listbox1.selecteditem as datarowview; <br/> MessageBox. Show ("text =" + DRV [listbox1.displaymember] + "; value =" + DRV [listbox1.valuemember]);



Listbox1.selectedvalue. tostring (). Trim ();


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: 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.