2

即使组合框填充了许多值,默认情况下组合框也会显示空白字段

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

3 回答 3

0

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

于 2013-09-05T01:29:48.740 回答
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。

于 2010-07-30T22:00:06.270 回答
0

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

于 2011-01-11T06:01:55.870 回答