DataGridView AutoFit and Fill

前端 未结 10 604
孤独总比滥情好
孤独总比滥情好 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:58

    This is my favorite approach...

    _dataGrid.DataBindingComplete += (o, _) =>
        {
            var dataGridView = o as DataGridView;
            if (dataGridView != null)
            {
               dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
               dataGridView.Columns[dataGridView.ColumnCount-1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            }
        };
    
    0 讨论(0)
  • 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 <dataGridView1.Width)
            {
                dataGridView1.Columns["first_name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            }
            else if (dataGridView1.Columns["first_name"].Width < 10) dataGridView1.Columns["first_name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
        }
    
    0 讨论(0)
  • 2020-12-02 07:59
    public void setHeight(DataGridView src)
    {
        src.Height= src.ColumnHeadersVisible ? src.ColumnHeadersHeight : 0 +   src.Rows.OfType<DataGridViewRow>().Where(row => row.Visible).Sum(row => row.Height);
    }
    
    0 讨论(0)
  • 2020-12-02 08:02

    Try this :

      DGV.AutoResizeColumns();
      DGV.AutoSizeColumnsMode=DataGridViewAutoSizeColumnsMode.AllCells;
    
    0 讨论(0)
  • 2020-12-02 08:05

    To build on AlfredBr's answer, if you hid some of your columns, you can use the following to auto-size all columns and then just have the last visible column fill the empty space:

    myDgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
    myDgv.Columns.GetLastColumn(DataGridViewElementStates.Visible, DataGridViewElementStates.None).AutoSizeMode = 
        DataGridViewAutoSizeColumnMode.Fill;
    
    0 讨论(0)
  • 2020-12-02 08:08

    Not tested but you can give a try. Tested and working. I hope you can play with AutoSizeMode of DataGridViewColum to achieve what you need.

    Try setting

    dataGridView1.DataSource = yourdatasource;<--set datasource before you set AutoSizeMode
    
    //Set the following properties after setting datasource
    dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
    dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
    dataGridView1.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
    

    This should work

    0 讨论(0)
提交回复
热议问题