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. |