0

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

在此处输入图像描述

这似乎不是由 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() 方法似乎不是罪魁祸首。我将其注释掉并看到了相同的工件。

4

1 回答 1

1

非常感谢 TaW 和这个网站:

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

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

ResizeRedraw = true;

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

有趣的是 this.EditingControlDataGridView.EditingControl.Width 和 e.ClipRectangle.Width 是不同的。不同之处在于添加到右侧的“额外”空间。如果我只允许在它们相等时进行绘画,则在右侧可以观察到一个空白区域。

于 2014-04-11T14:37:09.080 回答