Hi,
I have a custom DataGridViewColumn which uses custom editing control to edit values of cells. The editing control shows up in a edited cell when I want to edit a cell :), and that works just as it should. The problem is that my editing control is much bigger than the cell itself - is there any way to show editing control outside/'above' the cell being edited? Thanks in adv.

Dani AI

Generated

Short answer: the grid uses two different rectangles for positioning — the cell bounds (the logical cell rectangle) and the clip/paint rectangle (the visible area used to size and clip the editing panel). Because the DataGridView computes an EditingPanel and then positions the editing control inside that panel using offsets based on both rectangles, changing only one leaves the other out of sync and the control gets clipped or offset incorrectly. This matches what observed and explains why adjusting both cellBounds and cellClip fixed the problem. (learn.microsoft.com)

More detail (why it matters): the framework first calculates an editing‑panel rectangle (the container) and then computes the editing‑control rectangle inside that panel. The editing control’s location inside the panel is derived from the difference between the two rectangles (after border/padding adjustments). If you move or grow cellBounds but leave cellClip unchanged, the panel and the control end up with inconsistent coordinates and part (or all) of the editor is clipped. The .NET source shows this exact arithmetic for panel size/position and control offset. (dotnetframework.org)

Practical guidance: in your override clone the incoming rectangles, update both consistently, and use the framework helper (base.PositionEditingPanel or the base cell positioning methods) to get the in‑panel bounds; then set DataGridView.EditingControl.Location/Size from that result. Clone before mutating (as suggested) so you do not change caller state. If you truly need a floating editor that escapes the grid’s clipping, host the editor outside the grid (ToolStripDropDown or a small popup Form) and implement IDataGridViewEditingControl to sync values and handle scroll/resize/end‑edit events — otherwise keep the editor inside the EditingPanel and update/hide it on grid scroll/resize. See Microsoft’s guidance about hosting editors in DataGridView cells for recommended patterns. (learn.microsoft.com)

Recommended Answers

All 4 Replies

Assuming you are passing the rectangle of the control being edited to the custom edit control, you should just need to adjust Rectangle.Location Property. Be sure to create a clone of the rectangle before changing the location so you don't modify the original.

Thanks for your reply DdoubleD! What I did (Isolved my problem partially already) was overriding the PositionEditingControl(...) method of the DataGridViewTextBoxCell:

class CustomCell : DataGridViewTextBoxCell
    {
        public override Type EditType
        {
            get
            {
                //CustomEditingControl is the editing control I want to appear for this cell when it is in 'edit mode'
                return typeof(CustomEditingControl);
            }
        }
        public override void PositionEditingControl(bool setLocation, bool setSize, System.Drawing.Rectangle cellBounds, System.Drawing.Rectangle cellClip, DataGridViewCellStyle cellStyle, bool singleVerticalBorderAdded, bool singleHorizontalBorderAdded, bool isFirstDisplayedColumn, bool isFirstDisplayedRow)
        {
           //Here I change position and size to fit the size of the editing control
            Control c = DataGridView.EditingControl;
            //size
            cellBounds.Height = c.Height;
            cellBounds.Width = c.Width;
            cellClip.Height = c.Height;
            cellClip.Width = c.Width;

            //position
            //move editing control down, so it shows below DataGridCell
            cellBounds.Y += this.Size.Height;
            cellClip.Y += this.Size.Height;

            base.PositionEditingControl(setLocation, setSize, cellBounds, cellClip, cellStyle, singleVerticalBorderAdded, singleHorizontalBorderAdded, isFirstDisplayedColumn, isFirstDisplayedRow);
        }
    }

But I do not quite understand /I came to this solution by trying different things/ why I need to change values of 'cellBounds' as well as 'cellClip' parameters (changing only one of those does not make the editing control visible 'above' the cell being edited). Anyone can explain me the meaning of those values and the difference between them? - the one MSDN provides doesn't make it clearer for me. Thanks.

Not sure that you need to change the clip region, but it could be related to the tab order of the form's controls because leave and focus events do not fire off always in that order, sometimes producing undesirable affects. Just mentioning it to be aware of because I ran into a similar issue with a floating textbox on a treeview control that would sometimes be hidden underneath the treeview caused by this.

For this to work I have to set both cellClip and cellBounds (why?;), but now it works and perhaps why doesn't matter. Thank you again DdoubleD!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.