Programmatically set selected index of a ComboBox in DataGridView

后端 未结 1 1435
星月不相逢
星月不相逢 2021-01-15 18:18

I want to, while avoiding databinding, to set the selected index in a ComboBox in a DataGridView. It is not connected to a DB.

All solutions I have found have the Da

1条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-15 18:35

    Since the DataGridViewComboBoxColumn has no SelectedIndex or SelectedValue properties, you can try and set the value like this example:

    DataGridViewComboBoxColumn cmbCurrencies = (DataGridViewComboBoxColumn)myDataGridView.Columns["ComboboxCurrencyColumn"];
    
    var currencies = entities.currencies.Select(c => c.currencyName).DefaultIfEmpty().ToList();     
    
    cmbCurrencies.DataSource = currencies; 
    

    and then:

    for (int i = 0; i <= myDataGridView.RowCount - 1; i++)
        {
             myDataGridView.Rows[i].Cells["Index of Combobox Column"].Value = "Pound";
        }
    

    See also if this may help.

    0 讨论(0)
提交回复
热议问题