2010-01-23 24 views

回答

9

一個黑客一點,但你可以試試這個:

dataGridView1.BackgroundColor = System.Drawing.SystemColors.Control; 

順便說一句這已經reported as a bug

+0

這有效,但你仍然有邊界。任何想法如何刪除? – 2014-11-29 11:51:14

11

這是可以做到,你就必須一行時添加或移除來調節ClientSize。然而,它並不能完全一旦垂直滾動條出現,電網高度不是由行高度divisble隱藏背景。爲您的項目添加一個新類並粘貼下面顯示的代碼。編譯。將新的控件從工具箱的頂部拖放到表單上。

using System; 
using System.Drawing; 
using System.Windows.Forms; 

class AutoSizeGrid : DataGridView { 
    private int gridHeight; 
    private bool resizing; 
    protected override void OnClientSizeChanged(EventArgs e) { 
    if (!resizing) gridHeight = this.ClientSize.Height; 
    base.OnClientSizeChanged(e); 
    } 
    protected override void OnRowsAdded(DataGridViewRowsAddedEventArgs e) { 
    setGridHeight(); 
    base.OnRowsAdded(e); 
    } 
    protected override void OnRowsRemoved(DataGridViewRowsRemovedEventArgs e) { 
    setGridHeight(); 
    base.OnRowsRemoved(e); 
    } 
    protected override void OnHandleCreated(EventArgs e) { 
    this.BeginInvoke(new MethodInvoker(setGridHeight)); 
    base.OnHandleCreated(e); 
    } 
    private void setGridHeight() { 
    if (this.DesignMode || this.RowCount > 99) return; 
    int height = this.ColumnHeadersHeight + 2; 
    if (this.HorizontalScrollBar.Visible) height += SystemInformation.HorizontalScrollBarHeight; 
    for (int row = 0; row < this.RowCount; ++row) { 
     height = Math.Min(gridHeight, height + this.Rows[row].Height); 
     if (height >= gridHeight) break; 
    } 
    resizing = true; 
    this.ClientSize = new Size(this.ClientSize.Width, height); 
    resizing = false; 
    if (height < gridHeight && this.RowCount > 0) this.FirstDisplayedScrollingRowIndex = 0; 
    } 
} 
+0

THX爲你的努力,但我得到了通過使用上述建議的方法擺脫空間。 – 2010-01-23 17:06:27

+0

我在測試中發現了一些問題,請看:http://prnt.sc/b3rf7g我也可以刪除灰色小行嗎?根據數據,左手角落有一個很大的graw區域:http://prnt.sc/b3rft4我該如何解決這個問題? – Jack 2016-05-13 22:53:54

3

設置數據網格的MaxHeight屬性。 e.g MaxHeight="150"

在我來說,我已經刪除了你在用紅色邊境以上電網已經證明的空間。