How do you automatically resize columns in a DataGridView control AND allow the user to resize the columns on that same grid?

后端 未结 24 3067
孤城傲影
孤城傲影 2020-11-29 18:03

I am populating a DataGridView control on a Windows Form (C# 2.0 not WPF).

My goal is to display a grid that neatly fills all available width with cells - i.e. no un

24条回答
  •  星月不相逢
    2020-11-29 18:45

    Slightly neater C# code from Miroslav Zadravec's code assuming all columns are to be autosized

    for (int i = 0; i < dgvProblems.Columns.Count; i++)
    {
        dgvProblems.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
        int colw = dgvProblems.Columns[i].Width;
        dgvProblems.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
        dgvProblems.Columns[i].Width = colw;
    }
    

提交回复
热议问题