Description:
Is it possible to determine the coordinates of the grid cells and column headers in my GridView object? How can I find out how many rows or columns are visible within the grid control's boundaries?
Answer:
On occasion, you may find it necessary to obtain drawing information about XtraGrid elements, such as the coordinates of cells and columns. This is possible via the ViewInfo property of a given view (BaseView) object.
If you are using the GridView, you should obtain the GridViewInfo object to access specific information about the grid's visual elements. The GridViewInfo class is not documented. However, there is nothing difficult involved in using it.
The BaseView class provides a GetViewInfo method since XtraGrid v3.2.0. The result of the GetViewInfo method must be typecast to the appropriate ViewInfo class. For example, if you are using the GridView, you can write the following code to obtain its ViewInfo object:
C#using DevExpress.XtraGrid.Views.Grid.ViewInfo;
GridViewInfo info = gridView1.GetViewInfo() as GridViewInfo;
Visual BasicImports DevExpress.XtraGrid.Views.Grid.ViewInfo
Dim info As GridViewInfo
info = CType(GridView1.GetViewInfo(), GridViewInfo)
Earlier versions did not provide access to the ViewInfo object and thus you had to use the classes of the System.Reflection namespace. Below is the necessary code. Please note how the fViewInfo protected field is accessed via the Type.GetField and FieldInfo.GetValue methods.
C#using System.Reflection;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
GridViewInfo GetGridViewInfo(GridView view) {
FieldInfo fi;
fi = typeof(GridView).GetField("fViewInfo", BindingFlags.NonPublic | BindingFlags.Instance);
return fi.GetValue(view) as GridViewInfo;
}
Visual BasicImports System.Reflection
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid.Views.Grid.ViewInfo
Function GetGridViewInfo(ByVal view As GridView) As GridViewInfo
Dim fi As FieldInfo
fi = GetType(GridView).GetField("fViewInfo", BindingFlags.NonPublic Or BindingFlags.Instance)
Return CType(fi.GetValue(view), GridViewInfo)
End Function
See Also:
How to obtain grid cell coordinates
How to determine the number of visible rows in the XtraGrid
How to align summary values displayed in a group row to the corresponding columns