DataGridView:神秘的第二次下拉列表

时间:2014-04-08 17:34:30

标签: c# winforms datagridview .net-3.5 datagridviewcomboboxcell

当我的数据网格视图中的下拉组合框的编辑控件浮出水面,并且用户展开宽度时,会出现一个辅助组合框。

enter image description here

这似乎不是由AutoGeneratedColumns引起的,因为我将其设置为false。

protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
{
    base.OnCellPainting(e);

    if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
    {
        DataGridViewRow currentRow = this.Rows[e.RowIndex];
        DataGridViewCell currentCell = currentRow.Cells[e.ColumnIndex];
        DataItem item = currentRow.DataBoundItem as DataItem;
        if (item != null)
        {
            if (new string[] { "Property1", "Property2" }.Contains(this.Columns[e.ColumnIndex].DataPropertyName))
            {
                EnableDisableCell(currentCell, item);
            }
        }
    }
}

private void EnableDisableCell(DataGridViewCell cell, DataItem boundItem)
{
    if (cell != null)
    {
        switch (cell.OwningColumn.DataPropertyName)
        {
            case "Property1":
                if (boundItem.AllowP1Assignment)
                {
                    cell.Enable();
                }
                else
                {
                    cell.Disable();
                }
                break;

            case "Property2":
                if (boundItem.AllowP2Assignment)
                {
                    cell.Enable();
                }
                else
                {
                    cell.Disable();
                }
                break;
            default:
                break;
        }

    }
}

修改

EnableDisableCell()方法似乎不是罪魁祸首。我评论了它并看到了相同的文物。

1 个答案:

答案 0 :(得分:1)

非常感谢TaW和本网站:

http://www.csharp-examples.net/redraw-control-on-resize/

我只需要将它添加到构造函数中:

ResizeRedraw = true;

显然你也可以在涉及调整大小的事件中调用Invalidate()来调整整个事物的大小。

当这个消失时,有趣的是this.EditingControlDataGridView.EditingControl.Width和e.ClipRectangle.Width是不同的。区别在于"额外"增加到右边的空间。如果我只允许在它们相同的情况下进行绘画,则可以在右侧观察到空白的空白区域。