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]);
Or
Listbox1.selectedvalue. tostring (). Trim ();