Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How do I make the last column of a DataGrid fill the remaining client area?

If you have added a TableStyle for your grid, then the code below should set the right column width to be the empty space from a button click. If you need to dynamically do this in response to the user sizing other columns, then there may be more work. But if you only need to do it at the end of your Form_Load, then this code might be sufficient. It assumes your datasource is a datatable. You can download a sample project (C#, VB).

private void button1_Click( object sender, EventArgs e )
{
  int numCols = ( (DataTable) dataGrid1.DataSource ).Columns.Count;
  //the fudge -4 is for the grid borders
  int targetWidth = dataGrid1.ClientSize.Width -
    SystemInformation.VerticalScrollBarWidth - 4;
  int runningWidthUsed = dataGrid1.TableStyles[ "customers" ].RowHeaderWidth;

  for ( int i = 0; i < numCols - 1; ++i )
    runningWidthUsed +=
      dataGrid1.TableStyles[ "customers" ].GridColumnStyles[ i ].Width;

  if ( runningWidthUsed < targetWidth )
    dataGrid1.TableStyles[ "customers" ].GridColumnStyles[ numCols - 1 ].Width =
      targetWidth - runningWidthUsed;
}

Contributed from George Shepherd's Windows Forms FAQ