DatagridView: Remove unused space?

前端 未结 8 1125
-上瘾入骨i
-上瘾入骨i 2021-01-11 15:25

I was wondering whether it is possible to remove the unused space ( the gray space ) of the DataGridView control in C#. I have to make the DataGridView

相关标签:
8条回答
  • 2021-01-11 15:47
    dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
    
    0 讨论(0)
  • 2021-01-11 15:49

    Sometimes (especially with winforms) the best way is to hack:

    dataGridView1.BackgroundColor = System.Drawing.SystemColors.Control;
    

    I stole it from this post: removing the empty gray space in datagrid in c#

    0 讨论(0)
  • 2021-01-11 15:59

    Well, I toiled to find an answer for this before but in the end if you want to mimic an empty DataGridView then the long answer is to create "White" Rectangle objects and use Graphics to fill the whole grid on an overriden OnPaint method.

    0 讨论(0)
  • 2021-01-11 16:02

    go to the designer:

    1) change datagridview background color same as the form color

    2) set datagridview "BorderStyle" to None

    0 讨论(0)
  • 2021-01-11 16:06

    Set the RowsHeaderVisible property to false, you can either do that from the designer, in category Appearence, or from the code :

    dataGridView1.RowsHeaderVisible = false;
    

    In order to remove the indicator row on the left side, as for the rest of the grey space, you can try set the aforementionned AutoSizeColumnsMode to Fill, but you will still have the lower part grayed out from lack of rows.

    Instead of sizing your cells to fill your grid, you could resize your grid in order to fit around your cells. Whether or not this is an acceptable approach will depend on your intent.

    I mean, it's possible that if its just the color that is bothering you, setting the backcolor to white would do the trick.

    0 讨论(0)
  • 2021-01-11 16:06

    I have found no simple way to remove the "unused" or gray (BackgroundColor) space. However, an effective solution for me was to hide the borders of the DataGridView and to change its background color to the background of the surrounding control. In essence, the perception is that there is no more unused space.

    Here is a snippet in pseudocode:

    TableGridView = DataGridView()
    TableGridView.Width = 0
    TableGridView.Height = 0
    TableGridView.AutoSize = true 
    TableGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
    TableGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
    TableGridView.BackgroundColor = SystemColors.ControlLightLight
    TableGridView.BorderStyle = BorderStyle.None
    

    I read somewhere that the AutoSize setting is not applicable, however, it did change things for me. This example suggests that the surrounding control has a background color of SystemColors.ControlLightLight, but this can be modified as required.

    Please vote this up if it helped you.

    0 讨论(0)
提交回复
热议问题