C#
  Home arrow C# arrow Page 2 - 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 - The Problems Faced with the DataGrids


    (Page 2 of 3 )

    The Problems Faced

    Trying to rebuild a control can not always be done as easily as we would sometimes like. There are several problems that need to be overcome when trying to implement the new features.  For starters, the first uneditable column has width. This makes the calculation of the actual grid width a bit difficult. The picture above shows this area, the column before the ‘ID’ column.

    Next, the DataGridColumnStyles that make up the DataGrid do not have a Visible property, thus making them hard to hide. And for the resizing of the rows, you really don’t want to know how that works.  Although the final code is quite easy, there were some problems with re-sorting and filtering the DataGrid; however, those problems are solved now.  Let’s handle each problem separately.

    Calculating the Grid Width

    While calculating the width of the grid can be easily achieved, there is only one setback: there must be rows present in the viewed DataSource.  This is because the width of the first uneditable column is calculated by obtaining the X position of the first cell. No cell, no width. When there are no rows, a default value is used.

    Hiding the Grid Columns

    Each column in the DataGrid is represented by a DataGridColumnStyle object. This is the object that houses the width property which the columns adhere to.  After working with the DataGrid I noticed that it’s impossible for the user to resize a column to a width of zero. So it’s possible for us to use that width to make the column invisible. We can also make use of this for checking whether a column is visible or not. This is necessary because we don’t want the auto sizing behavior to resize hidden columns, making them visible again. Setting the width to zero hides the column and will act like a switch when auto resizing the column widths.

    Resizing the Row Heights

    This was the hardest nut to crack. The first problem that I faced was finding out how the sizing of a single row works. Searching the documentation turned up a method in the DataGridColumnStyle called GetMinimumHeight; this, of course, peaked my interest.
     
    I started playing around with the method, first making it return a higher value than normal. Wham! The rows resize. This really is the method that does the work! Now that the right method has been found, we need to find out for which cell GetMinimumHeight is called for; or maybe it’s called on a row-by-row basis?

    Finding out was a bit harder than it would seem. The GetMinimumHeight does not supply any parameters that can be used, possibly leaving it next to impossible to guess which cell is queried for its height.  The best way to solve this problem is adding debug statements and playing around with the code.  We need to find the pattern of the GetMinimumHeight calling mechanism. When the pattern is known, it will be possible to link this pattern to specific rows or cells. The debug statements revealed the calling pattern quickly enough. It’s on a cell-by-cell basis, quite logical because we’re working with columns here.

    The second problem was how to obtain the correct text to measure. Again the DataGridColumnStyle has a method available that might help. It’s called GetColumnValueAtRow, and it will need a CurrencyManager whatever that is) for obtaining the values. This now shifts our problem to obtaining a CurrencyManager.  The documentation helps out again.  In the documentation an example of how to obtain the CurrencyManager is shown.  I tried it out, and did obtain correct string values. 

    Nice, second problem solved.

    It is now possible to implement our own mechanism. It will need to count how many times the GetMinimumHeight method is called, and return a string height using the GetColumnValueAtRow method and a CurrencyManager obtained from the grid. When the count reaches the number of rows in the displayed source, the counter will need to be reset. This will allow for more dynamic behavior (not doing so will totally ruin the program when sorting, adding, or filtering, try it out).
    The resetting of the counter was the last problem with which I was faced.  At times, the DataSource wouldn’t return the right number.  After trying some other possibilities, the CurrencyManager turned out to be the correct source for the information required. 

    The code below is a cutout from the overridden GetMinimumHeight method, it shows the simplicity of the mechanism, and is all that is needed for row height sizing!

       // Get CurrencyManager
        
    CurrencyManager cur 
    (
    CurrencyManager)this.DataGridTableStyle.
          
    DataGrid
    .BindingContext[this.DataGridTableStyle.DataGrid.DataSource,
          
    this
    .DataGridTableStyle.DataGrid.DataMember];
        
    // Rows 
    available?
        
    if(cur == null || cur.Count == 
    0)
          
    return 
    base.GetMinimumHeight();
        
    // Increment 
    counter
        this
    .currentIteration++;
        
    // 
    Initialize return value
        int retVal 

    base.GetMinimumHeight();
        
    // Calculate height of row at 
    currentIteration 1
        retVal 

    this.CalcStringHeight(GetColumnValueAtRow(cur,
          
    currentIteration 
    1).ToString());
       
    // Reset when last cell 
    reached
       
    if(currentIteration == 
    cur.Count)
          this
    .ResetIterations(); // sets 
    currentIteration to 0
       
    // Return
       return 
    retVal

     

    More C# Articles
    More By Wouter Van Vugt


     

    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 1 hosted by Hostway
    Stay green...Green IT