joaocarlospf
joaocarlospf

Reputation: 1157

Adjust DataGridView's columns to fill available space if the grid is smaller and use scroll in case the grid bigger than the space available

I want to adjust all columns of a DataGridView according to the required space to show all its data completely. If the space required is smaller than the available space i want the grid to fill this exceeding space, but if the available is space is not enough to display properly all columns i want to DataGridView create automatically a scroll. Is there an easy way to do this?

Upvotes: 16

Views: 18883

Answers (5)

Prachi
Prachi

Reputation: 11

For adjusting column size when the number of columns in Datagridview are dynamic, need to find the width of the columns getting displayed in Datagrid. In designer set Autosize to DataGridViewAutoSizeColumnsMode.None

    Dim dgvBOMcolumnWidth As Integer = 0
    For Each gridcol As DataGridViewColumn In DgvBOMHeader.Columns
        If (gridcol.Visible) Then
            dgvBOMcolumnWidth = gridcol.Width + dgvBOMcolumnWidth
        End If
    Next

    If (dgvBOMcolumnWidth < DgvBOMHeader.Width) Then //if dynamic column width is less than actual grid width ,fill the columns.
        For Each gridcol As DataGridViewColumn In DgvBOMHeader.Columns
            gridcol.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
        Next
    Else
        DgvBOMHeader.AutoSize = DataGridViewAutoSizeColumnsMode.None
    End If

Upvotes: 1

the_cwazy_wabbit
the_cwazy_wabbit

Reputation: 91

If you want to keep your table (DataGridView) formatted such that all of the columns are automatically sized, but one column in particular fills the remaining space, you can do something like this:

//Store the number of columns in a variable
int columnCount = dataGridView.Columns.Count;

//If we want the last column to fill the remaining space
int lastColumnIndex = columnCount - 1;

//Loop through each column and set the DataGridViewAutoSizeColumnMode
//In this case, if we will set the size of all columns automatically, but have
//the last column fill any extra space available.
foreach(DataGridViewColumn column in dataGridView.Columns) 
{
    if (column.Index == columnCount - lastColumnIndex) //Last column will fill extra space
    {
        column.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
    }
    else //Any other column will be sized based on the max content size
    {
        column.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
    }
}

//Turn the scrollbars on for the DataGridView if needed
dataGridView.ScrollBars = ScrollBars.Both;

Upvotes: 5

Web Developer India
Web Developer India

Reputation: 677

Go to DataGridView Property select "AutoSizeColumnsMode". Set "Fill".

Upvotes: 2

Ramu
Ramu

Reputation: 353

DataGridView properties -> "AutoSizeMode" to "Fill" does fill up all the available space, however if a column is large enough to occupy majority of grid space, then rest of the columns size would shrink.

May be you can try "AutoSizeMode" to "AllCells". This not only fills up entire grid but also makes enough space to view all columns(with horizontal scroll bar)

Upvotes: 1

joaopintocruz
joaopintocruz

Reputation: 291

Click over the DataGridView and select "Edit Columns...", then go to "Layout" and set "AutoSizeMode" to "Fill".

Hope this is what you're looking for.

Cheers

Upvotes: 13

Related Questions