views:

349

answers:

4

In C#, I have a table displayed using a DataGridView. The table is significantly smaller than the form in which it appears, and so the table fills only a small portion of the upper-left-hand corner of the form.

My question: how can I (programmatically) make either: (1) the table automatically enlarge so that it fills the form, or (2) make the form automatically shrink to the size of the table? (And, are both possible?)

using System ;
using System.Windows.Forms ;
using System.Data ;

public class NiftyForm : System.Windows.Forms.Form
    {
    private  DataGridView  myDataGridView ;
    private  System.Data.DataTable myDataTable ;

    public NiftyForm ( )
     {
     this.Load  +=  new EventHandler ( NiftyFormLoadEventHandler ) ;
     }

    private void NiftyFormLoadEventHandler ( System.Object sender,
                              System.EventArgs ea )
     {
     this.Location  =  new System.Drawing.Point ( 40, 30 ) ;
     this.Size      =  new System.Drawing.Size ( 800, 600 ) ;

     myDataTable  =  new DataTable ( ) ;
        DataColumn  myDataColumn  =  new DataColumn ( ) ;
     myDataColumn.DataType   = typeof(string) ;
     myDataColumn.ColumnName   = "Name";
     myDataColumn.ReadOnly   = true;
     myDataTable.Columns.Add ( myDataColumn ) ;

        myDataColumn   =  new DataColumn ( ) ;
     myDataColumn.DataType = typeof(int) ;
     myDataColumn.ColumnName = "Age";
     myDataColumn.ReadOnly = true;
     myDataTable.Columns.Add ( myDataColumn ) ;

     string [ ]  Name  =  new string [ 5 ]
                    { "Dwight", "Abe", "Cal", "Bill", "Eisenhower" } ;
     int    [ ]  Age   =  new int    [ 5 ] { 123, 45, 6, 78, 9 } ;
     for ( int i = 0 ; i < 5 ; i ++ )
      {
      DataRow   myDataRow  =  myDataTable.NewRow ( ) ;
      myDataRow [ "Name" ] = Name [ i ] ;
      myDataRow [ "Age" ] = Age  [ i ] ;
      myDataTable.Rows.Add ( myDataRow ) ;
      }

     this.myDataGridView     = new DataGridView ( ) ;
     this.myDataGridView.DataSource   = myDataTable ;
     this.myDataGridView.Dock  = DockStyle.Fill ;
     this.Controls.Add ( this.myDataGridView ) ;
     }

    [ STAThreadAttribute ( ) ]

    static void Main ( )
     {
     Application.Run ( new NiftyForm ( ) ) ;
     }

    }
+1  A: 

Play with Dock and Anchor properties of the Grid :) Also, you might want to try AutoSize property of the form to see if it works as you'd like to.

Michał Chaniewski
+3  A: 
this.myDataGridView.Dock = DockStyle.Fill;

This will make the DataGrid fill the entire Form

You can also use an Anchor like so.

this.myDataGridView.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;

This means that the DataGrid will resize when the Form resizes.

Stan R.
AnchorStyles.Bottom is missing in your answer.
Eren Aygunes
That's because I didn't anchor it to the bottom of the form, was just showing how you can Anchor to select parts of the form :)
Stan R.
A: 

Set the form AutoSize=True and AutoSizeMode=GrowAndShrink and your form will size to the grid size.

C-Pound Guru
A: 

I have yet to meet a problem like this that I couldn't solve with the Anchor property. For me, Dock seems much harder to use. I guess if there are no other controls on the form, DockStyle.Fill is OK. Most forms tend to have other controls on them so, anchoring to all four sides is much easier.

NascarEd