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
display the white table only.
Any suggestions?
Note: This post originally contained an external image that is no longer valid
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.
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#
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With