At the same time binding encoding and name to ComboBox control (WinForm)

xiaoxiao2021-03-06  39

When using ComboBox, you often need to display the user name, but when you receive user information, you should receive the encoding of user selection. The following method implements the encoding and name simultaneously bind to the ComboBox control, and you can take the user-selected project name or Selected item encoding. // Generate new DataTable (can also be the result of query from the database) DATATABLE DT = New DataTable (); dt.columns.add ("id"); dt.columns.add ("Name ");

/ / Data DATATABLE DR = DT.NEWROW (); DR ["ID"] = "11"; DR ["Name"] = "aaaaa"; DT.ROWS.ADD (DR); DR = DT. NEWROW (); DR ["ID"] = "22"; DR ["name"] = "bbbbb"; dt.Rows.Add (DR);

// Bind display content and actual value comBobox1.valuemember = "id"; ComboBox1.displaymember = "name"; // Binding data combobox1.datasource = dt;

Finally Value ComboBox1.Text Take the display content comboBoX1.SelectedValue

转载请注明原文地址:https://www.9cbs.com/read-75189.html

New Post(0)