DataGridView AutoFit and Fill

前端 未结 10 593
孤独总比滥情好
孤独总比滥情好 2020-12-02 07:37

I have 3 columns in my DataGridView. What I am trying to do is have the first 2 columns auto fit to the width of the content, and have the 3rd column fill the r

10条回答
  •  有刺的猬
    2020-12-02 07:59

    This is what I have done in order to get the column "first_name" fill the space when all the columns cannot do it.

    When the grid go to small the column "first_name" gets almost invisible (very thin) so I can set the DataGridViewAutoSizeColumnMode to AllCells as the others visible columns. For performance issues it´s important to set them to None before data binding it and set back to AllCell in the DataBindingComplete event handler of the grid. Hope it helps!

    private void dataGridView1_Resize(object sender, EventArgs e)
        {
            int ColumnsWidth = 0;
            foreach(DataGridViewColumn col in dataGridView1.Columns)
            {
                if (col.Visible) ColumnsWidth += col.Width;
            }
    
            if (ColumnsWidth 

提交回复
热议问题