locked
Strange Anomaly in DataGridView Example RRS feed

  • Question

  • I have implemented the DataGridView example shown in the Microsoft Documentation for System.Windows.Forms at the URL: https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridview?view=netcore-3.1#examples

    Something strange happens when I use the cursor to widen the columns of the DataGridView control.  I am seeing vertical dotted tracks in the button Panel part of the form as shown in the bottom portion of the snapshot below.  I suspect I may need to handle the DataGridView ColumnWidthChanged event to invalidate the Panel to have it redrawn.  What seems strange is why does the DataGridView control affect a separate control on the form, namely, the Panel control?

    Added: My suggested fix seems to have worked.  I added an event handler for DataGridView ColumnWidthChanged, and in that event handler I invalidated the Panel control which allowed it to repaint itself to remove those strange lines.


    MesPia


    • Edited by MesPia Friday, October 16, 2020 7:26 PM Found solution
    Friday, October 16, 2020 7:10 PM

Answers

  • Hi MesPia,

    Thank you for posting here.

    The problem is related to ‘DockStyle.Fill’ 's behavior.

    If other controls also have DockStyle, for example,

    buttonPanel.Dock = DockStyle.Bottom

    the important thing is to add the control with DockStyle.Fill first in Controls then the others.

    You need to change the order:

                //SetupLayout();
                //SetupDataGridView();
    
                SetupDataGridView();
                SetupLayout();

    Or use Control.BringToFront method to makes the control be created last.

           songsDataGridView.Dock = DockStyle.Fill;
           songsDataGridView.BringToFront();

    Result of my test.

    Hope it could be helpful.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by MesPia Monday, October 19, 2020 1:02 PM
    Monday, October 19, 2020 2:35 AM

All replies

  • Hi MesPia,

    Thank you for posting here.

    The problem is related to ‘DockStyle.Fill’ 's behavior.

    If other controls also have DockStyle, for example,

    buttonPanel.Dock = DockStyle.Bottom

    the important thing is to add the control with DockStyle.Fill first in Controls then the others.

    You need to change the order:

                //SetupLayout();
                //SetupDataGridView();
    
                SetupDataGridView();
                SetupLayout();

    Or use Control.BringToFront method to makes the control be created last.

           songsDataGridView.Dock = DockStyle.Fill;
           songsDataGridView.BringToFront();

    Result of my test.

    Hope it could be helpful.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by MesPia Monday, October 19, 2020 1:02 PM
    Monday, October 19, 2020 2:35 AM
  • Thanks!  Very good suggestion.  It's kind of a ding against Microsoft since it was there example in the documentation.

    MesPia

    Monday, October 19, 2020 1:03 PM