Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Customize the Windows Forms DataGrid Control

The System.Windows.Forms.DataGrid control is an Excel-like component and designed to display data contained in .NET collections and list objects, including ADO.NET DataTable and DataView objects and arrays. The control provides scrolling capabilities, both vertically and horizontally, column resize, in-place editing, sorting, and even navigation, should your data source contain hierarchical information.


advertisement
hen I first met the ASP.NET DataGrid control, it was love at first sight. Together we built several applications, taught dozens of classes, published countless articles and tips; we even wrote a book. I can say with no fear of lying that we have an intimate knowledge of each other. When I need the Web DataGrid to perform some rather odd task, I only have to cast a glance (or two, if the task is quite complicated.) With this in mind, I enthusiastically accepted a proposal from one of my clients: Build a Windows Forms application with grid functionalities. Although at the time I had no serious experience with the Windows Forms DataGrid control, I took the gig because—I thought—a Windows DataGrid works more or less the same as an ASP.NET DataGrid. Next, hardly containing some genuine enthusiasm, I optimistically conjectured it could have been even easier—smart controls are handiest compared to HTML markup.

In the end it was a kind of nightmare. The Windows Forms DataGrid control is as much terrific to use as terrible to code. It is great as long as your needs match one of its built-in features. It can be hate at first sight; not love!

I thought a Windows DataGrid works more or less the same as an ASP.NET DataGrid. In the end, it was a kind of nightmare. The Windows Forms DataGrid control is as much terrific to use as terrible to code.
In this article, after a quick introduction to the overall capabilities of the control, I'll discuss how to achieve a basic customization made of custom column styles and improved user interaction. Next I'll take the plunge into the architecture of Windows Forms controls and discuss features and trends of the desktop .NET platform as well as possible strategies for us, poor developers, to defend.

The Windows Forms DataGrid at a Glance
The System.Windows.Forms.DataGrid control is an Excel-like component and designed to display data contained in .NET collections and list objects, including ADO.NET DataTable and DataView objects and arrays. The control provides scrolling capabilities, both vertically and horizontally, column resize, in-place editing, sorting, and even navigation should your data source contain hierarchical information.

If you need to display records out of a data source then the DataGrid is ideal. You run the query, gather the data into a DataTable, bind the object to the control, and you're done.

Dim adapter As New SqlDataAdapter(m_selectCommand, _ m_connectionString) Dim data As New DataSet() adapter.Fill(data, "Employees") grid.DataSource = data grid.DataMember = "Employees"


Any changes you enter in the bound data source is automatically persisted when you move out of the textbox, or when you call the EndEdit method. The underlying DataTable is guaranteed to be always up-to-date and, more importantly, you get all this for free.

But there's another really cool feature that you can obtain in a rather codeless way. Place two DataGrid controls on the same form and bind both to the same DataSet object. As long as the DataSet contains tables connected by a DataRelation object, you get automatic drill-down. You select a row on the grid that display the parent table and automatically the grid that bound to the child table refreshes to show related rows. Kind of magic!

However, don't forget that in this brief section I deliberately concentrated on all the best features of the controls. The worst is yet to come. Precisely, it begins when you—easily fired for these demonstrations—decide that you want to build the perfect grid control. Where do you start?



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date