开发者

Setting default value to a DataGridViewComboBoxCell

开发者 https://www.devze.com 2022-12-21 09:33 出处:网络
The combobox displays a blank field by default even though the combob开发者_运维知识库ox is populated with a number of values

The combobox displays a blank field by default even though the combob开发者_运维知识库ox is populated with a number of values

ColumnSpeed.DataSource = speedList;
ColumnSpeed.ValueType = typeof(string);

I also tried the following, but it still displays the blank text.

foreach (DataGridViewRow row in myDataGridView.Rows)
{
    DataGridViewComboBoxCell cell = row.Cells[ColumnSpeed.Index] as DataGridViewComboBoxCell;
    if (cell != null)
    {
        cell.DataSource = speedList;
        cell.Value = cell.Items[0].ToString();
    }   
}


It could be that the ValueMember you assigned to your DataGridView is different from the DisplayMember you assigned. If that's the case, you'll get a blank value, plus you'll get a DataGridError firing.

You should try:

foreach (DataGridViewRow row in dgMain.Rows){
DataGridViewComboBoxCell pkgBoxCell = row.Cells[ColumnSpeed.Index]

pkgBoxCell.Value = ((Package) pkgBoxCell.Items(0)).Id

}

I converted that from vb.net, so it may not compile. In place of the line where i set the value, do whatever steps are necessary to retrieve and set the correct ValueMember value. In my example, I am casting the item to a specific type and using it's id.


I believe the code you have written should work .. just want to know where are you calling the same. It should work if you call it in the databinding_complete event of the grid


Once you set all the DataSources, try calling the DataGridView.Refresh() method. This is usually required to display changes to the DataSources.

0

精彩评论

暂无评论...
验证码 换一张
取 消