Resizing Data Grid Columns To Content


This article was contributed by Tom Archer.

Environment: .NET, C#



Click here for a larger image.

Demo application to test datagrid resizing method

Introduction

One peculiar omission from the Windows Forms teams regarding the DataGrid object is a method that enables you to resize the grid's columns to visually accomodate the width of its widest data value. This method gives you that ability.

Acknowledgements

While searching for a solution to this problem, a friend emailed me some Visual Basic.NET code to accomplish the feat. While I could not get the code to compile (I'm assuming it was written during one of the .NET betas), I did re-write it in C# and got it to work. I'll update this article to give a proper acknowledgment once I track down the original author's name. However, I did want to make clear that some kind soul did get me 90% of the way there with their VB code and I finished the rest for my C# environment.

Using the Code

Syntax

Calling this method is very straightforward. It takes only two parameters:

public void SizeColumnsToContent(DataGrid dataGrid,
                                 int nRowsToScan)
  • dataGrid—The DataGrid object to be resized
  • nRowsToScan—The number of rows to scan when searching for the widest value for a given column. Passing a value of -1 indicates that all rows should be scanned.

Notes

  • In my own code, I have defined a method as a member of my own System.Windows.Forms.DataGrid-derived class (DataGridEx). I removed it from that class for this article to keep things simple. To use the method in the same manner that I did, simply remove the dataGrid parameter from the parameter list and replace the references to dataGrid with reference to the this object.
  • The reason for the nRowsToScan parameter is for performance issues regarding very large amounts of data. If you happen to have a datagrid with a large number of rows, you can simply have the grid's columns sized according to the widest column of the first n rows instead of the method iterating through the entire data set for each column.

SizeColumnsToContent Method

public void SizeColumnsToContent(DataGrid dataGrid,
       int nRowsToScan)
{
  // Create graphics object for measuring widths.
  Graphics Graphics = dataGrid.CreateGraphics();

  // Define new table style.
  DataGridTableStyle tableStyle = new DataGridTableStyle();

  try
  {
    DataTable dataTable = (DataTable)dataGrid.DataSource;

    if (-1 == nRowsToScan)
    {
      nRowsToScan = dataTable.Rows.Count;
    }
    else
    {
      // Can only scan rows if they exist.
      nRowsToScan = System.Math.Min(nRowsToScan,
                    dataTable.Rows.Count);
    }

    // Clear any existing table styles.
    dataGrid.TableStyles.Clear();

    // 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.
      tableStyle.GridColumnStyles.Add(columnStyle);
    }
    // Add the new table style to the data grid.
    dataGrid.TableStyles.Add(tableStyle);
  }
  catch(Exception e)
  {
    MessageBox.Show(e.Message);
  }
  finally
  {
    Graphics.Dispose();
  }
}

Downloads

Download demo project - 35 Kb
Download source - 2 Kb


About the Author

Tom Archer - MSFT

I am a Program Manager and Content Strategist for the Microsoft MSDN Online team managing the Windows Vista and Visual C++ developer centers. Before being employed at Microsoft, I was awarded MVP status for the Visual C++ product. A 20+ year veteran of programming with various languages - C++, C, Assembler, RPG III/400, PL/I, etc. - I've also written many technical books (Inside C#, Extending MFC Applications with the .NET Framework, Visual C++.NET Bible, etc.) and 100+ online articles.

Comments

  • Thanks

    Posted by sohaib on 07/04/2012 03:15am

    thanks for uploading this thing, i got my dataTable properly shown in Gridview by using the above function in my WIN CE 6.0 Device. .Net framework 3.5 Sohaib

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: October 29, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this eSeminar, Gene Kim will discuss these survey findings and will share woeful tales of artifact management gone wrong! Gene will also share examples of how …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds