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
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
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#
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.
go to the designer:
1) change datagridview background color same as the form color
2) set datagridview "BorderStyle" to None
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.
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.