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