Set ComboBox data source as column names

纵饮孤独 提交于 2019-12-11 04:26:42

问题


I have some table from a dataSet in a reportViewer (winform), and some ComboBox. I want the ComboBox's dataSource to be a list of the table column names. How can I do that?


回答1:


Assuming you have a data table in table variable, then to show list of it's columns in a ComboBox you can use a code like this:

comboBox1.DataSource = table.Columns.Cast<DataColumn>().ToList();
comboBox1.ValueMember = "ColumnName";
comboBox1.DisplayMember = "ColumnName";



回答2:


you can try this also

 comb.DataSource = dt;
 comb.ValueMember = dt.Columns[0].ColumnName.ToString();
 comb.DisplayMember = dt.Columns[1].ColumnName.ToString();


来源:https://stackoverflow.com/questions/56001568/set-combobox-data-source-as-column-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!