C# DataGridView:神秘的第二个下拉列表

C# DataGridView:神秘的第二个下拉列表,c#,winforms,datagridview,.net-3.5,datagridviewcomboboxcell,C#,Winforms,Datagridview,.net 3.5,Datagridviewcomboboxcell,当出现my datagrid视图中下拉组合框的编辑控件,并且用户扩展宽度时,会出现一个辅助组合框 这似乎不是由AutoGeneratedColumns引起的,因为我将其设置为false protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e) { base.OnCellPainting(e); if (e.RowIndex >= 0 && e.ColumnInd

当出现my datagrid视图中下拉组合框的编辑控件,并且用户扩展宽度时,会出现一个辅助组合框

这似乎不是由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()方法似乎不是罪魁祸首。我对它进行了评论,看到了同样的作品。

非常感谢TaW和这个网站:

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

ResizeRedraw = true;
显然,您也可以在涉及调整大小的事件中调用Invalidate(),它将调整整个事件的大小


当它消失时,有趣的事情是这个。EditingControlDataGridView.EditingControl.Width和e.ClipRectangle.Width是不同的。不同之处在于添加到右侧的“额外”空间。如果我只允许在它们相等的情况下进行绘制,则可以在右侧看到空白空白。

这只是绘制错误还是两者都起作用?@Taw:似乎是绘制错误,因为下拉列表始终与左侧垂直柱线齐平(并且从不与右侧组合框齐平)。此外,OnPaint调用始终具有相同的this.GetHashCode()值。OnPaint调用之间的宽度也不同。可能使DGV或单元格区域无效会使其消失。。?但是在绘画事件中改变单元格的启用状态对我来说肯定是可疑的;这应该在任何绘画之前发生。。