KB Article A2825
Visible to All Users

How to change the Grid's height according to the total height of its rows

Description:
Is it possible to change the Grid's height so that only its rows are visible in the content area and there is no empty space.

Answer:
Unfortunately, there is no easy way to achieve this task. However, you can implement it using reflections. Please see the sample below:

C#
using System.Reflection; using DevExpress.XtraGrid.Scrolling; using DevExpress.XtraGrid.Views.Base; using DevExpress.XtraGrid.Views.Grid; using DevExpress.XtraGrid.Views.Grid.ViewInfo; //... private void UpdateGridSize() { DevExpress.XtraGrid.Views.Grid.ViewInfo.GridViewInfo viewInfo = (GridViewInfo)gridView1.GetViewInfo(); FieldInfo fi = typeof(GridView).GetField("scrollInfo", BindingFlags.Instance | BindingFlags.NonPublic); ScrollInfo scrollInfo = (ScrollInfo)fi.GetValue(gridView1); int height = viewInfo.CalcRealViewHeight(new Rectangle(0, 0, Int32.MaxValue, Int32.MaxValue)); if (scrollInfo.HScrollVisible) height += scrollInfo.HScrollRect.Height; gridControl1.Height = height; gridView1.LayoutChanged(); }
Visual Basic
Imports System.Reflection Imports DevExpress.XtraGrid.Scrolling Imports DevExpress.XtraGrid.Views.Base Imports DevExpress.XtraGrid.Views.Grid Imports DevExpress.XtraGrid.Views.Grid.ViewInfo '... Private Sub UpdateGridSize() Dim viewInfo As DevExpress.XtraGrid.Views.Grid.ViewInfo.GridViewInfo = CType(gridView1.GetViewInfo(), GridViewInfo) Dim fi As FieldInfo = GetType(GridView).GetField("scrollInfo", BindingFlags.Instance Or BindingFlags.NonPublic) Dim scrollInfo As ScrollInfo = DirectCast(fi.GetValue(gridView1), ScrollInfo) Dim _height As Integer = viewInfo.CalcRealViewHeight(New Rectangle(0, 0, Int32.MaxValue, Int32.MaxValue)) If scrollInfo.HScrollVisible Then _height += scrollInfo.HScrollRect.Height End If gridControl1.Height = _height gridView1.LayoutChanged() End Sub

NOTE:
In versions prior to 6.1 use the following code to obtain the GridViewInfo and ScrollInfo objects:

C#
Type type = gridView1.GetType(); FieldInfo fi = type.GetField("fViewInfo", BindingFlags.NonPublic | BindingFlags.Instance); GridViewInfo info = fi.GetValue(gridView1) as GridViewInfo; fi = type.GetField("fScrollInfo", BindingFlags.NonPublic | BindingFlags.Instance); ScrollInfo scrollInfo = fi.GetValue(gridView1) as ScrollInfo;
Visual Basic
Dim type As Type = GridView1.GetType() Dim fi As FieldInfo = type.GetField("fViewInfo", BindingFlags.NonPublic Or BindingFlags.Instance) Dim info As GridViewInfo = CType(fi.GetValue(GridView1), GridViewInfo) fi = type.GetField("fScrollInfo", BindingFlags.NonPublic Or BindingFlags.Instance) Dim scrollInfo As ScrollInfo = CType(fi.GetValue(GridView1), ScrollInfo)

See also:
How to change the Grid's height according to the total height of its rows

Comments (3)

    Hi Stan,

    If I use this code twice or more on the same grid, I can not use this grid anymore.
    It do not proper react on mouse clicks e.g…
    Can you reproduce this behavior?

    Is there a solution?

    Regards
    Dirk

      I change die GridExport Example from your VB.Net GridTutorials.

      Nadezhda (DevExpress Support) 7 years ago

        Hello,

        I've created a separate ticket on your behalf (How to resize GridControl when the button is clicked). It has been placed in our processing queue and will be answered shortly.

        Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

        Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.