2010-03-01 88 views
2

组合框默认显示一个空白领域,即使组合框填入一个数值设置默认值到的DataGridViewComboBoxCell

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

我也试过以下的,但它仍然显示空白文本。

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(); 
    } 
} 
+1

嗨,我试图玩DataGridView,它适用于我的两种方式。你能提供更多的代码吗? – Ikaso 2010-03-01 19:50:11

回答

0

可能是您分配给DataGridView的ValueMember与您分配的DisplayMember不同。如果是这样的话,你会得到一个空值,再加上你会得到一个DataGridError触发器。

你应该尝试:

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

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

}

我转换,从vb.net,所以它可能不会编译。代替设置值的行,请执行任何必要的步骤来检索并设置正确的ValueMember值。在我的示例中,我将该项目转换为特定类型并使用它的id。

0

我相信你写的代码应该工作..只是想知道你在哪里叫相同。如果在网格的databinding_complete事件中调用它,它应该可以工作

0

一旦设置了所有的DataSources,就尝试调用DataGridView.Refresh()方法。通常需要显示对数据源的更改。