0

When the editing control for a drop down combo box in my datagrid view is surfaced, and the user expands the width, a secondary combo box appears.

enter image description here

This doesn't appear to be caused by AutoGeneratedColumns since I have that set to 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;
        }

    }
}

EDIT:

The EnableDisableCell() method doesn't seem to be the culprit. I commented it out and saw the same artifacts.

micahhoover
  • 2,101
  • 8
  • 33
  • 53
  • 1
    is this just a paint error or are they both functional? – TaW Apr 08 '14 at 20:27
  • @Taw: Appears to be a paint error since the drop down is always flush against the left hand vertical column line (and never flush with the right combo box). Also the OnPaint call always has the same this.GetHashCode() value. Also the widths vary between OnPaint calls. – micahhoover Apr 11 '14 at 14:04
  • 1
    Maybe Invalidating the DGV or the Cell region would make it go away..? But changing the Cell's enabled state in the Paint event certainly looks suspicious to me; this ought to have happened before any painting.. – TaW Apr 11 '14 at 14:13

1 Answers1

1

Many thanks to TaW and this website:

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

I just needed to add this to the constructor:

ResizeRedraw = true;

Apparently you can also call Invalidate() in events that involve resizing it will resize the whole thing.

The interesting thing about when this is gone is this.EditingControlDataGridView.EditingControl.Width and e.ClipRectangle.Width are different. The difference is the "extra" space that got added to the right. If I only allowed painting whent they were equal a blank white space was observable on the right side.

micahhoover
  • 2,101
  • 8
  • 33
  • 53