Bug 4177 - System.Windows.Forms.DataGridViewColumn.GetPreferredWidth always returns 0
Summary: System.Windows.Forms.DataGridViewColumn.GetPreferredWidth always returns 0
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: Windows.Forms ()
Version: master
Hardware: Other All
: Lowest normal
Target Milestone: Community
Assignee: Bugzilla
Depends on:
Reported: 2012-04-01 15:40 UTC by knittl89+bugs
Modified: 2017-09-01 11:55 UTC (History)
2 users (show)

Tags: mono-community
Is this bug a regression?: ---
Last known good build:

Avoid exception by returning at least MinimumWidth (1.16 KB, patch)
2012-07-26 11:03 UTC, knittl89+bugs

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report for Bug 4177 on GitHub or Developer Community if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: GitHub Markdown or Developer Community HTML
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

Description knittl89+bugs 2012-04-01 15:40:43 UTC
All code paths of System.Windows.Forms.DataGridViewColumn.GetPreferredWidth() return 0 (or throws an Exception in case of an invalid DataGridViewAutoSizeColumnMode).

This makes it impossible to use this function to set the width of columns in a DataGridView, since an exception is thrown as soon as Width is set to PreferredWidth (= 0) which is usually less than MinimumWidth (defaults to 5)

Expected Results:
GetPreferredWidth should return a valid Width value (at least MinimumWidth instead of 0?)

How often does this happen? Every time.
Comment 1 knittl89+bugs 2012-07-26 06:01:51 UTC
Here's a simple patch, that does exactly that: return MinimumWidth instead of 0. It's still not perfect (and rows might be too narrow), but at least it does not crash.
The patch should apply without problems to current master

From 50e3b8223431058d713c5dbabd0eb9d47608e7c8 Mon Sep 17 00:00:00 2001
From: Daniel Knittl-Frank <knittl89+git@googlemail.com>
Date: Sun, 1 Apr 2012 22:08:10 +0200
Subject: [PATCH] Return at least MinimumWidth instead of 0 in

Otherwise we cannot use GetPreferredWidth as new Width, since this will
throw an Exception (ArgumentOutOfRange)

Signed-off-by: Daniel Knittl-Frank <knittl89+git@googlemail.com>
 .../Managed.Windows.Forms/System.Windows.Forms/DataGridViewColumn.cs  | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/mcs/class/Managed.Windows.Forms/System.Windows.Forms/DataGridViewColumn.cs b/mcs/class/Managed.Windows.Forms/System.Windows.Forms/DataGridViewColumn.cs
index 93801d1..d248674 100644
--- a/mcs/class/Managed.Windows.Forms/System.Windows.Forms/DataGridViewColumn.cs
+++ b/mcs/class/Managed.Windows.Forms/System.Windows.Forms/DataGridViewColumn.cs
@@ -475,10 +475,10 @@ Example */
 				throw new ArgumentException("AutoSizeColumnMode is invalid");
 			if (fixedHeight) {
-				return 0;
+				return MinimumWidth;
 			else {
-				return 0;
+				return MinimumWidth;
Comment 2 knittl89+bugs 2012-07-26 11:03:22 UTC
Created attachment 2262 [details]
Avoid exception by returning at least MinimumWidth