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

后端 未结 24 3066
孤城傲影
孤城傲影 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:44

    A C# version of Miroslav Zadravec's code

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

    Posted as Community Wiki so as to not mooch off of the reputation of others

提交回复
热议问题