C#
  Home arrow C# arrow Page 3 - Automatic Row Height in DataGrids
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Automatic Row Height in DataGrids
By: Wouter van Vugt
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 19
    2004-01-14

    Table of Contents:
  • Automatic Row Height in DataGrids
  • The Problems Faced with the DataGrids
  • DataGrid Solutions and Article's Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Automatic Row Height in DataGrids - DataGrid Solutions and Article's Conclusion


    (Page 3 of 3 )

    The Solution Offered

    As the introduction stated, the new DataGrid implements autosizing behavior. In this section I will explain the functions offered.

    First let’s talk a bit about the auto width. The grid will attempt to resize the columns so they fit exactly in the ClientSize.Width. It will resize all columns equally, except those that have been resized by the user; those columns will stay their set size (read the acknowledgement section about this idea). The columns will never automatically resize smaller than the PreferredColumnWidth, which can be set in the DataGrid.

    Hiding the columns is performed by setting their width to zero. A ContextMenu is provided for showing and hiding the columns. It can be brought up by right clicking anywhere in the grid. It also houses two resetting options, one for the width of the columns, the other for resetting the auto size behavior of columns that have been sized by the user.

    The row heights are performed automatically.  The only thing that has to be done is adding the right DataGridTableStyle (one with the supplied DataGridColumnStyle for each column) to the TableStyles collection of the grid.  This can be done by calling the style creation method supplied with the grid.

    There is a demo project supplied which shows a form, a grid and some buttons. It demonstrates that automatic resizing works while sorting, filtering or adding new rows. Play with it and be amazed! (I hope...)

    Problems

    As for problems, there is only one small one so far.  Do not change the ReadOnly property of the grid to false. This will cause the row counter for the auto height to fail. You could do it, but expect to recalculate the currentIteration counter in the DataGridColumnStyle supplied in the source.  You probably won’t want to make those changes; my User Interface Design teacher once told me “DataGrids are for viewing ONLY.”

    Acknowledgement

    The idea to resize all columns to equal sizes, and overriding the autosizing when the user has resized the column was thought up by Mike Griffin.  Read all about his DataGrid at
    http://www.c-sharpcorner.com/Code/2003/Nov/DynamicDataGrid.asp

    Thank you Mike!  I hope you like the row sizing.

    Conclusion

    Finally, a DataGrid with some proper sizing functions is implemented and now available. I found some resizing algorithms on the internet, but there was never one available that resized the row heights, which is my biggest annoyance.  Now it’s available and ready for the next challenge: text wrapping.  That’s just around the corner!

    I tested the new control, but now it faces its biggest test, you.  Please let me know of any problems that arise while using this control.  Again, never, ever set the ReadOnly property to false unless you want trouble.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    C# ARTICLES

    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#
    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT