?
Solved

Sizing data grid contents to fit data

Posted on 2003-03-16
3
Medium Priority
?
1,266 Views
Last Modified: 2012-05-04
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?
0
Comment
Question by:tma050898
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 

Expert Comment

by:Lagi
ID: 8146915
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 
0
 
LVL 3

Author Comment

by:tma050898
ID: 8147088
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?
0
 
LVL 3

Accepted Solution

by:
tma050898 earned 0 total points
ID: 8160907
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();
     
       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();
       }
     }
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question