Make DataGridView Auto-Height according to the Rows


I need to be able to accommodate the height of FORM or DataGridView based on the amount of data / rows that were filled in DataGrid .

This taking into account what cases there are four rows, and others that there are only two rows or three, where there is a blank space that does not occupy the rows of DataGridView .

Example 1 : In this example, the query returns four rows, and the space is almost complete:

Example 2 : In this example, the query returns only three rows and the space is much larger:

The idea is that, no matter if the query returns "n" row numbers , the space is the exact so that all rows are visible instantly , then, there is no need to use ScrollBar

How could I achieve this?




Thank you in advance to those who participated and helped me: D

The code is shared:

Code in C#

        /// <summary>
        /// Función que se encarga de redimencionar el alto del formulario deacuerdo a la cantidad de datos que posee el datagrid view
        /// </summary>
        private void RedimencionarForm()
            int AltoGridIni = Grid_Datos.Height;
            int AltoGrid = 0;
            int AltoForm = this.Height;
            int Diferencia = 0;
            AltoGrid = AltoGrid + Grid_Datos.ColumnHeadersHeight;

            for (int i = 0; i <= Grid_Datos.Rows.Count - 1; i++)
                AltoGrid = AltoGrid + Grid_Datos.Rows[i].Height;
            Diferencia = AltoGridIni - AltoGrid;

            if (Diferencia > 0)
                AltoForm = AltoForm - Diferencia;
                this.Height = AltoForm;
            else if (Diferencia < 0)
                AltoForm = AltoForm + Diferencia;
                this.Height = AltoForm;
            Grid_Datos.Height = AltoGrid;
asked by Fabian Montoya 16.05.2017 в 17:49

1 answer


The solution to your problem can be found in the code that I am going to share with you, only that looking at the images that you shared obviously it is necessary that you adapt the code to your needs.

Dim DT_Prueba As New DataTable

' Teniendo el DataTable que tienes cargado previamente en tu DataGridView   '
' Este paso no es necesario que lo coloques ya que solo lleno un DataTable  '
' con Información y lo asigno al DataGridView                               '

For x = 0 To 10

DataGridView1.DataSource = DT_Prueba

' Aqui comienza el código de interés                                        '
' Defino una variable para almacenar el alto final del Grid                 '
Dim AltoDelGrid As Integer

' Sumo el alto de la fila que representa el encabezado de las Columnas      '
AltoDelGrid = AltoDelGrid + DataGridView1.ColumnHeadersHeight

' Ahora recorro el DataGridView y sumo el alto de cada fila                 '
For x = 0 To DataGridView1.Rows.Count - 1
    AltoDelGrid = AltoDelGrid + DataGridView1.Rows(x).Height

' Finalmente asigno el valor de la variable al alto del control             '
DataGridView1.Height = AltoDelGrid
answered by 17.05.2017 / 16:23