Raz*_*ter 2 c# datagridview winforms
我正在使用 Datagridview,通过设置 DataPropertyNames 部分由数据源填充。部分是我在代码中填写的。我有一个 DataGridViewComboBoxColumn,当我单击它旁边的按钮时,应填充其中的单元格。单击按钮填充 DataGridViewComboBoxCell 的项目,但如果我单击 ComboBox,则无法打开它来选择值。
(在函数末尾,我将列值更改为 x 作为测试,这工作正常......)
代码:
//add the combobox column
DataGridViewComboBoxColumn cellcol = new DataGridViewComboBoxColumn();
cellcol.Name = "Cell";
cellcol.ReadOnly = false;
dataGridView1.Columns.Add(cellcol);
//method that gets called when the button is clicked
private void getAllCells(List<ComponentRow> componentRows, int i)
{
//show all cells
CellFilter cf = new CellFilter();
cf.customerNr = libraryNr;
Cell[] cells = Service.Instance.Client.queryCells(cf, new QueryParam());
foreach (Cell c in cells)
{
((DataGridViewComboBoxCell)dataGridView1["Cell", i]).Items.Add(cells[0].cell_name);
}
cellsShownForComponents[i] = ShowedCells.ALL;
((DataGridViewTextBoxCell)dataGridView1["variants", i]).Value = "x";
}
Run Code Online (Sandbox Code Playgroud)
我会回答我自己的问题。问题是我的 datagridview 是只读的。无法打开只读组合框列。我这样修复它:对于 datagridview
ReadOnly = false
Run Code Online (Sandbox Code Playgroud)
对于我的组合框列
ReadOnly = false
Run Code Online (Sandbox Code Playgroud)
对于所有其他列,用户不应该能够编辑
ReadOnly = true
Run Code Online (Sandbox Code Playgroud)