Skip to main content

Colonnes automatiques sur un DataGridView

En Windows Forms 3.5 et 2.0, le DataGridView affiche par défaut toutes les colonnes de la source de données qui lui est liée. Vous pouvez configurer les colonnes dans le mode Design de Visual Studio 2008, mais à première vue impossible de revenir en arrière. Je vous explique ici comment revenir à la situation d'origine.

Comme son cousin le GridView d'ASP.NET, le DataGridView des Windows Forms sait détecter les colonnes de la DataSource qui lui est passée. Si vous avez (ou que Visual Studio l'a fait pour vous) configuré les colonnes d'un DataGridView mais que vous voulez revenir en arrière. Le DataGridView a une propriété AutoGenerateColumns qu'il suffit de remettre à "True" (sa valeur par défaut), mais malheureusement elle n'apparaît pas dans la fenêtre de propriétés de Visual Studio 2008. Comment faire donc? Suivez le guide...

Commencez par supprimer les colonnes en mode Design. Pour cela, cliquez sur le smart tag en haut à droite du DataGridView: image

Dans le menu du smart tag, choisissez "Modifier les colonnes", et dans la boîte de dialogue qui s'ouvre supprimez toutes les colonnes. Cliquez sur le bouton "OK" pour fermer la boîte de dialogue.

Notez le nom du GridView. Dans mon exemple, c'est categoriesDataGridView. Dans l'explorateur de solutions, ouvrez le fichier généré par Visual Studio pour votre formulaire. Par exemple, si le formulaire s'appelle MasterDet.cs, vous devez ouvrir le fichier MasterDet.Designer.cs:

image

Recherchez les lignes de définition du contrôle et supprimez la ligne qui indique "AutoGenerateColumns=false" (celle qui est surlignée dans ma copie d'écran):

image

Et voilà!

Note: je déconseille d'habitude fortement d'intervenir à la main dans le fichier généré par Visual Studio (xxx.Designer.cs), car vous n'avez rien à y faire et Visual Studio risque de ne plus s'y retrouver. Mais il me semble que c'est la seule manière de remettre la propriété AutoGenerateColumns (c'est sidérant, mais bon...).

Comments

Popular posts from this blog

Gift idea : help your loved ones take their IT skills to the next level

You surely know a special person who works in IT. Learn Kubernetes & Docker enables them to quickly ramp up on those two technologies that one doesn't want to miss in 2021. With smoother operations, better scaling and availability, DevOps tooling, containers and containers orchestration smartly solve many problems that developers and system administrators commonly face. Don't let your loved ones face problems in their IT job : offer them the book that takes them from zero to productive in a matter of days. Grab it now! Get the ebook from Leanpub , Amazon or choose from many retailers . Get the print edition from Lulu .  

Learn Meteor book available

I'm pleased to announce the general release of my Learn Meteor book. It is now available as an ebook or print book from various sources: Learn Meteor print (paperback) on Lulu Learn Meteor ebook on LeanPub Learn Meteor ebook on Barnes & Noble Learn Meteor ebook on iBooks Learn Meteor ebook on Kobo Learn Meteor ebook on Scribd Learn Meteor ebook on Inktera Page Foundry Learn Meteor ebook on 24symbols Learn Meteor ebook on Amazon US Learn Meteor ebook on Amazon UK Learn Meteor ebook on Amazon France Learn Meteor ebook on Amazon Deutschland Learn Meteor ebook on Amazon Canada Learn Meteor ebook on Amazon India Learn Meteor ebook on Amazon Brasil Learn Meteor ebook on Amazon Mexico Learn Meteor ebook on Amazon España Learn Meteor ebook on Amazon Italia Learn Meteor ebook on Amazon Netherlands Learn Meteor ebook on Amazon Japan Learn Meteor ebook on Amazon Australia More sources are coming soon for the print version. Learn Meteor has been a fun expe...

Learn ASP.NET Core: read the book as it is being written

     TL;DR: get the discounted book now using the coupon , then get the full version for free. Thanks to the great acceptance of my previous books, ( Learn WPF MVVM ,  Learn ASP.NET MVC and Learn Meteor ), I'm in the process of writing a fourth one. Learn ASP.NET Core - MVC and DI with .NET Core 1.1 using Visual Studio 2017 teaches you how to quickly get coding using that technology. Just as I did previously, I'm publishing it before it is even finished. Would you like to read it? You can get it for half the price: 50% off the book using that coupon (limited to the first 100 readers and up to august 1st 2017): http://leanpub.com/netcore/c/K9mHH0IzfI2F About 50% of the book is already written, yet it is already available for download as an ebook: PDF, EPUB, MOBI: you choose your format. Over the next days I'll be writing the rest of the book, and publishing updates often. The final book will be ready by au...