The Artima Developer Community
Sponsored Link

.NET Buzz Forum
How do I automatically size (autosize) Columns in a WinForms DataGrid?

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Scott Hanselman

Posts: 1031
Nickname: glucopilot
Registered: Aug, 2003

Scott Hanselman is the Chief Architect at Corillian Corporation and the Microsoft RD for Oregon.
How do I automatically size (autosize) Columns in a WinForms DataGrid? Posted: Jun 18, 2004 4:06 AM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Scott Hanselman.
Original Post: How do I automatically size (autosize) Columns in a WinForms DataGrid?
Feed Title: Scott Hanselman's ComputerZen.com
Feed URL: http://radio-weblogs.com/0106747/rss.xml
Feed Description: Scott Hanselman's ComputerZen.com is a .NET/WebServices/XML Weblog. I offer details of obscurities (internals of ASP.NET, WebServices, XML, etc) and best practices from real world scenarios.
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Scott Hanselman
Latest Posts From Scott Hanselman's ComputerZen.com

Advertisement

Apparently this is, like, the problem for the ages or something, but noone on this planet has come up with a way to automatically size the columns in a DataGrid.  Sure, there's all sorts of pyscho ways that involve measuring the length of strings in DataSets with the Graphics context, yada yada.  But, since I'm binding strongly-typed Object Collections to DataGrids in WinForm apps, that doesn't work for me (and it's a little over the top, IMHO).

So, I thought about it like this:

  • If you double click on the little splitter between columns they will autosize.
  • Therefore, the code to autosize has been written for me; no need to measure strings, etc.
  • How do I force a double click? No, wait, wrongheadedness, how do I call whatever THEY call when a double click happens?
  • So, I reflectored into DataGrid.OnMouseDown and saw their custom HitTest calls a private ColAutoResize.  Ah, bingo.

If you're going to 'sin' do it with style - do it with Reflection.

private void dgLogging_DataSourceChanged(object sender, System.EventArgs e)
        {
            try
            {
                Type t = dgLogging.GetType();
                MethodInfo m = t.GetMethod("ColAutoResize",BindingFlags.NonPublic);

                for (int i = dgLogging.FirstVisibleColumn; (i < dgLogging.VisibleColumnCount); i++)
                {
                    m.Invoke(dgLogging, new object[]{i});
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.Write("Failed Resizing Columns: " + ex.ToString());
            }
        }

(Thanks to Patrick Cauldwell for his help)

Read: How do I automatically size (autosize) Columns in a WinForms DataGrid?

Topic: AlternateHeader magic, Reporting Services Web Parts Previous Topic   Next Topic Topic: The Israeli Agile development workshop - New site

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use