开发者

Setting default value to a DataGridViewComboBoxCell

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜