title: Windows Forms: How to make the Form and a Grid Change Sizes Together?
body: <p>In C#, I have a table displayed using a <code>DataGridView</code>.  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.</p>
  
  <p>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?)</p>
  
  <pre><code>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 &lt; 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 ( ) ) ;
          }
  
      }
  </code></pre>