开发者

How the Rightmost column of a DataGridView could fill the remaining part of the form?

开发者 https://www.devze.com 2023-04-03 21:46 出处:网络
I have a DataGridView wi开发者_运维百科th 5 columns, with dock = filled. I want the last (rightmost) column\'s width to fill the remaining right side of the form, when the form is maximized.

I have a DataGridView wi开发者_运维百科th 5 columns, with dock = filled.

I want the last (rightmost) column's width to fill the remaining right side of the form, when the form is maximized.

How can I do that?


Set the AutoSizeMode of the (rightmost) column to Fill


For anyone who is AutoGenerating their columns (or even if you're not), you can call this extension method on your DataGridView to resize the last column to Fill

C-Sharp:

public static class Utilities
{
    public static void StretchLastColumn(this DataGridView dataGridView)
    {
        var lastColIndex = dataGridView.Columns.Count - 1;
        var lastCol = dataGridView.Columns[lastColIndex];
        lastCol.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
    }
}

Visual Basic:

Public Module Utilities

    <Extension()>
    Public Sub StretchLastColumn(ByVal dataGridView As DataGridView)
        Dim lastColIndex = dataGridView.Columns.Count - 1
        Dim lastCol = dataGridView.Columns.Item(lastColIndex)
        lastCol.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
    End Sub

End Module
0

精彩评论

暂无评论...
验证码 换一张
取 消