问题
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.
alt text http://www.timvw.be/wp-content/images/datagridview-to-excel-1.gif
Any suggestions ?
回答1:
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#
回答2:
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.
回答3:
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.
回答4:
I believe you want:
myDataGrid.AutoSizeColumnsMode = Fill
EDIT: This just resizes the columns. I'm not sure how you would get rid of row gray space other than resizing the grid's height.
回答5:
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
回答6:
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.
回答7:
go to the designer:
1) change datagridview background color same as the form color
2) set datagridview "BorderStyle" to None
来源:https://stackoverflow.com/questions/2162756/datagridview-remove-unused-space