C# – Add extra column to fill out space in datagridview C#

cdatagridviewnetwinforms

I have a datagridview which im binding DataTable to. What I want do is add an extra column which will fill out the remaining gap in the windows form. At the moment I only have 3 columns so the width of all the columns is only about half the size of the windows form.

Best Answer

After databinding the DataTable to the DataGridView, set the desired column's AutoSizeMode to Fill.

        DataTable dt = new DataTable("Table1");
        dt.Columns.Add("A");
        dt.Columns.Add("B");
        dt.Columns.Add("C");
        dt.Rows.Add(1, 2, 3);
        this.dataGridView1.DataSource = dt;
        this.dataGridView1.Columns[dataGridView1.Columns.Count - 1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

You may also want to set the DataGridView to Anchor to the Right and Bottom sides of the form (as well as the left and top) so that the DGV gets bigger as the form is resized. (or set Dock to Fill).