Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DatagridView: Remove unused space?

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

like image 639
Seb Avatar asked Jan 29 '10 15:01

Seb


2 Answers

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.

like image 161
W1M0R Avatar answered Oct 20 '22 03:10

W1M0R


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#

like image 41
BornToCode Avatar answered Oct 20 '22 03:10

BornToCode