开发者

DatagridView: Remove unused space?

开发者 https://www.devze.com 2022-12-18 06:58 出处:网络
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.

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 开发者_JAVA技巧post originally contained an external image that is no longer valid


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#


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.


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 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.


dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;


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


You must set a fixed row-height, then make the height of the DGV to be an exact multiple of the row height, PLUS a couple of pixels.

    DGV1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None                                  
    DGV1.RowTemplate.Height = 25                                                               
    DGV1.Height = 252

You will then always show a complete number of rows, plus a bottom tiny margin that will not be gray. To then discover which rows are showing, use:

    Dim firstVisibleRowIndex As Integer = DGV1.FirstDisplayedCell.RowIndex
    Dim lastVisibleRowIndex As Integer = firstVisibleRowIndex + DGV1.DisplayedRowCount(False) - 1


I use this code for and it works for my if you don't add a button column or image I take it from a site but I don't remember from where :

For Each row As DataGridViewRow In DataGridView1.Rows
If datagrid_limits > newHeight Then
newHeight =newHeight + 40
Else
Exit For
End If
Next
DataGridView1.Height = newHeight
0

精彩评论

暂无评论...
验证码 换一张
取 消