• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1276
  • Last Modified:

Sizing data grid contents to fit data

Using MFC, I can do this by iterating through the list control's columns and setting each column's width to either the match the header's content or the largest row's content (depending on which is larger).

However, I can't find any DataColumn method that allows me to do this. Can a .NET data grid column be set to auto size to its widest value?
  • 2
1 Solution
You should use TableStyle. You should generate a table style (mapped to you table/stored procedure ...) and attack it to the grid.
In the tablestyle you have column style. You may set the width of the column style there.

You can see it on : http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q792q 
tma050898Author Commented:
This shows how to manually set a column to a hard-coded pixel size. How can I tell the grid to autosize to the widest row value?
tma050898Author Commented:
It took about 8 hours, but I finally got this to work. Jeez! About 100 lines of code to simply have a gird size to its contents! The .NET folks definitely fell down on this one. Here's the code:

     void SizeDataGridColumnsToContent(DataGrid dataGrid, int nRowsToScan)
       // Create graphics object for measuring widths.
       Graphics Graphics = dataGrid.CreateGraphics();
       // Define new table style.
       DataGridTableStyle tableStyle = new DataGridTableStyle();
         DataTable dataTable = (DataTable)dataGrid.DataSource;
         if (-1 == nRowsToScan)
           nRowsToScan = dataTable.Rows.Count;
           // Can only scan rows if they exist.
           nRowsToScan = System.Math.Min(nRowsToScan, dataTable.Rows.Count);
         // Clear any existing table styles.
         // Use mapping name that is defined in the data source.
         tableStyle.MappingName = dataTable.TableName;
         // Now create the column styles within the table style.
         DataGridTextBoxColumn columnStyle;
         int iWidth;
         for (int iCurrCol = 0; iCurrCol < dataTable.Columns.Count; iCurrCol++)
           DataColumn dataColumn = dataTable.Columns[iCurrCol];
           columnStyle = new DataGridTextBoxColumn();

           columnStyle.TextBox.Enabled = true;
           columnStyle.HeaderText = dataColumn.ColumnName;
           columnStyle.MappingName = dataColumn.ColumnName;
           // Set width to header text width.
           iWidth = (int)(Graphics.MeasureString(columnStyle.HeaderText, dataGrid.Font).Width);

           // Change width, if data width is wider than header text width.
           // Check the width of the data in the first X rows.
           DataRow dataRow;
           for (int iRow = 0; iRow < nRowsToScan; iRow++)
             dataRow = dataTable.Rows[iRow];
             if (null != dataRow[dataColumn.ColumnName])
               int iColWidth = (int)(Graphics.MeasureString(dataRow.ItemArray[iCurrCol].ToString(), dataGrid.Font).Width);
               iWidth = (int)System.Math.Max(iWidth, iColWidth);
           columnStyle.Width = iWidth + 4;
           // Add the new column style to the table style.
         // Add the new table style to the data grid.
       catch(Exception e)

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now