DataGridViewCell.ReadOnly = false不起作用

时间:2014-03-15 17:47:19

标签: c# datagridview datagridviewcomboboxcell datagridviewtextboxcell

好的,所以我有这段代码,我尝试将所有行设置为只读一些。

由于条件每行更改,我将所有单元格设置为ReadOnly,然后解锁我需要的单元格。

没有涉及数据绑定。我使用网格"按原样#34;。

cells [1]是DataGridViewTextBoxCell

cells [4]是DataGridViewComboBoxCell

当我这样做时:

row.ReadOnly = true;
row.Cells[1].ReadOnly = false; //Successfully changes from true to false.
row.Cells[4].ReadOnly = false; // This DOES NOT work!

好的,所以我尝试了别的东西:

foreach (DataGridViewCell cell in row.Cells)
{
    cell.ReadOnly = true; //Successfully changes from false to true.
}
row.Cells[1].ReadOnly = false; // This DOES NOT work!
row.Cells[4].ReadOnly = false; // This DOES NOT work!

这里是列定义:

textBoxColumn = new DataGridViewTextBoxColumn();
textBoxColumn.Name = "Column0";
textBoxColumn.HeaderText = "My column header";
textBoxColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
textextBoxColumn.Resizable = DataGridViewTriState.True;
textextBoxColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
dataGridView.Columns.Add(textBoxColumn);

comboBoxBoxColumn = new DataGridViewComboBoxColumn();
comboBoxBoxColumn.Name = "Column4";
comboBoxBoxColumn.HeaderText = "My column header";
comboBoxBoxColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
comboBoxBoxColumn.Resizable = DataGridViewTriState.True;
comboBoxBoxColumn.Items.Add("Item 1");
comboBoxBoxColumn.Items.Add("Item 2");
comboBoxBoxColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
dataGridView.Columns.Add(comboBoxBoxColumn);

我只是不明白。

修改

这是我创建行的方式: 我首先创建所有(数百个)行,然后将其一次性添加到DataGridView中,以避免UI刷新:

row = new DataGridViewRow();
for (Int32 i = 0; i < dataGridView.Columns.Count; i++)
{
    DataGridViewCell cell;
    DataGridViewColumn column;

    column = dataGridView.Columns[i];
    cell = (DataGridViewCell)(column.CellTemplate.Clone());
    row.Cells.Add(cell);
}

0 个答案:

没有答案