Live Chat Icon For mobile
Live Chat Icon

How can I make my last column wide enough to exactly occupy all the client area of the datagrid

Platform: WinForms| Category: Datagrid

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, System.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 = this.dataGrid1.TableStyles[''customers''].RowHeaderWidth; 
      
  for(int i = 0; i < numCols - 1; ++i)
    runningWidthUsed += this.dataGrid1.TableStyles[''customers''].GridColumnStyles[i].Width;

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

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.