Setting DataGridViewComboBoxColumn.Valuemember to

2019-06-14 05:12发布

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条回答
Animai°情兽
2楼-- · 2019-06-14 06:08

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.

查看更多
登录 后发表回答