Example E1811
Visible to All Users

WinForms Data Grid - How to change the grid's height according to the total height of its rows

This example dynamically changes the height of the grid to match the total height of the visible rows.

The GridControl.CalcBestSize method calculates the maximum allowable size of the Grid Control (to display as many data rows and columns as possible).

Files to Review

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

GridControlAutoSize/Form1.cs(vb)
C#
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using DevExpress.XtraEditors; using DevExpress.XtraGrid.Views.Grid.ViewInfo; using DevExpress.XtraGrid.Columns; using System.Reflection; using DevExpress.XtraGrid.Views.Grid; using DevExpress.XtraGrid.Scrolling; using DevExpress.XtraGrid.Views.Base; namespace GridControlAutoSize { public partial class MainForm : XtraForm { private const int GridMinWidth = 400; private const int GridMinHeight = 200; public MainForm() { InitializeComponent(); } private void MainForm_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'nwindDataSet.Orders' table. You can move, or remove it, as needed. this.ordersTableAdapter.Fill(this.nwindDataSet.Orders); gridView1.ActiveFilterString = "CustomerID = 'ALFKI'"; } private void UpdateGridSize() { Size maxSize = new Size(ClientSize.Width, ClientSize.Height); gridControl1.Size = gridControl1.CalcBestSize(maxSize, false); gridControl1.MainView?.LayoutChanged(); gridControl1.Size = gridControl1.CalcBestSize(maxSize, true); gridControl1.MainView?.LayoutChanged(); } private void OnGridViewRowCountChanged(object sender, EventArgs e) { UpdateGridSize(); } private void OnGridViewColumnWidthChanged(object sender, ColumnEventArgs e) { UpdateGridSize(); } private void OnGridViewDragObjectDrop(object sender, DragObjectDropEventArgs e) { UpdateGridSize(); } private void MainForm_Shown(object sender, EventArgs e) { gridView1.RowCountChanged += new EventHandler(OnGridViewRowCountChanged); gridView1.ColumnWidthChanged += new ColumnEventHandler(OnGridViewColumnWidthChanged); gridView1.DragObjectDrop += new DragObjectDropEventHandler(OnGridViewDragObjectDrop); UpdateGridSize(); } } }

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.