Setting DataGridViewComboBoxColumn.Valuemember to a list<string>

吃可爱长大的小学妹 提交于 2019-12-12 10:22:05

问题


I needed to know if one can set the ValueMember property of a DataGridViewComboBoxColumn directly from a list of strings.

e.g.

List<string> productNames = new List<string>();

List<Products.Product> t = new List<Products.Product>();

foreach (var p in products)
{
     var x = p.Product;

     itemListing = x;

     foreach (var pn in x)
     {
         productNames.Add(pn.name);
     }
}
.............

// set values to combobox column cells in datagridview
GridSellProducts.Rows.Add();
DataGridViewComboBoxColumn cmbItems = (DataGridViewComboBoxColumn)GridSellProducts.Columns["Item"];

cmbItems.DataSource = productNames;
cmbItems.DisplayMember = cmbItems.ValueMember;
cmbItems.ValueMember = // code to put here  
cmbItems.AutoComplete = true;

Didn't quite get the example in http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcolumn.datasource.aspx

How does one set cmbItems.ValueMember?


回答1:


I think you should do something like this:

cmbItems.DataSource = products;
cmbItems.DisplayMember = cmbItems.ValueMember = "name";

Now, each item is a Product with a property called name. In your old code, it's just a string so the only possible property for DisplayMember and ValueMember is Length, however it's not relevant and meaningless in this case I think.



来源:https://stackoverflow.com/questions/20144884/setting-datagridviewcomboboxcolumn-valuemember-to-a-liststring

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