views:

1698

answers:

3

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.

+3  A: 

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).

foson
Only reason I can see why this got downvoted is that it doesn't precisely answer the OP "how to add extra column" - even though it's an even better solution. :( +1 from me, anyway...
GalacticCowboy
A: 

foson answer was perfect. Thanks alot.

instigator
A: 
        dataGridView1.ColumnCount = 3;
        dataGridView1.Columns[0].Name = "data1";
        dataGridView1.Columns[1].Name = "data2";
        dataGridView1.Columns[2].Name = "data3";

find full source code here

http://csharp.net-informations.com/datagridview/csharp-datagridview-add-column.htm

gever

geveruk