2011-05-13 57 views
1

我有一个DataGridView,我选择DataGridView所在的选项卡时,将DataSource设置为对象集合。DataGridView - ComboBox @ runtime

数据源设置完成后,我想将“县”列更改为DataGridViewComboBoxColumn,并将州的县作为项目。我见过的没有任何作品 - 它总是给出一个例外。

有没有办法改变列类型而不删除它?

当我单击该行中的某个单元而不必修改列类型时,是否有办法让组合框或其他控件出现?我的失败代码=================== =====

//ii is a for loop variable on the dataGridViewFields.Columns.Count 

string colName = dataGridViewFields.Columns[ii].Name; 
if (colName.Equals("County")) 
{ 
    string dpName = dataGridViewFields.Columns[ii].DataPropertyName; 
    DataGridViewComboBoxColumn column = new DataGridViewComboBoxColumn(); 
    //get current column characteristics. 
    column.ValueType  = dataGridViewFields.Columns[ii].ValueType; ; 
    column.Name   = dataGridViewFields.Columns[ii].Name; 
    column.HeaderText = dataGridViewFields.Columns[ii].HeaderText; 
    column.Width   = dataGridViewFields.Columns[ii].Width; 
    //remove column from grid 
    dataGridViewFields.Columns.RemoveAt(ii); 
    //set column combobox characteristics 
    column.DropDownWidth = 160; 
    column.MaxDropDownItems = 10; 

    column.Items.AddRange(new string[] {"Dane", "Dodge", "Door"}); 
    column.FlatStyle = FlatStyle.Flat; 

    DataGridViewCell cell = new DataGridViewComboBoxCell(); 
    cell.Style.BackColor = Color.Wheat; 
    cell.ValueType = typeof(string); 
    cell.Value = "Door"; 
    column.CellTemplate = cell; 
    //add to the grid. 
    dataGridViewFields.Columns.Insert(ii, column); 
    dataGridViewFields.Columns[ii].DataPropertyName = dpName; 
    //dataGridViewFields.EditMode = DataGridViewEditMode.EditProgrammatically; 
} 
+0

为什么不能使用ComboBox列? – V4Vendetta 2011-05-13 11:41:54

回答

1

我想你会感兴趣的组合框列

集的DisplayStyle的DisplayStyleNothing。这将使组合框仅显示当您尝试编辑单元格

+0

@ user751254为您工作 – V4Vendetta 2011-06-17 05:22:23