PC Review Forums Newsgroups Microsoft DotNet Microsoft Dot NET Framework Forms .NET Winform Datagrid w/ dynamic row height (multiline) and word w

Reply

.NET Winform Datagrid w/ dynamic row height (multiline) and word w

 
Thread Tools Rate Thread
Old 14-12-2005, 10:40 PM   #1
=?Utf-8?B?TSBGaW5sZXk=?=
Guest
 
Posts: n/a
Default .NET Winform Datagrid w/ dynamic row height (multiline) and word w


It took me a long time to figure this out and I found many threads that were
looking for the same information. So...I thought I would post the sample
code I put together to solve the problem.

Below: Form to test datagrid, custom datagrid control, custom datagrid
textbox column, text file used in sample form

//*****************Form******************
using System;
using System.Data;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace DynamicColumn
{
/// <summary>
/// Summary description for FrmTest.
/// </summary>
public class FrmTest : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private AutoSizeDataGrid grid;
private Button button;
int clickcount = 0;
int idcount = 0;
private System.Windows.Forms.CheckBox chkReadOnly;
private System.Windows.Forms.Panel panel1;
DataTable table;

public FrmTest()
{
InitializeComponent();
table = new DataTable("blah");
table.Columns.Add("Column1",table.TableName.GetType());
table.Columns.Add("Column2",table.TableName.GetType());


grid = new AutoSizeDataGrid();
grid.ReadOnly = false;

grid.Dock = DockStyle.Fill;
panel1.Controls.Add(grid);
grid.CreateAutoSizeTableStyle(table,true);
grid.DataSource = table;

chkReadOnly.Checked = grid.ReadOnly;
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

private int nextID()
{
idcount++;
return idcount;
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button = new System.Windows.Forms.Button();
this.chkReadOnly = new System.Windows.Forms.CheckBox();
this.panel1 = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// button
//
this.button.Dock = System.Windows.Forms.DockStyle.Top;
this.button.Location = new System.Drawing.Point(0, 0);
this.button.Name = "button";
this.button.Size = new System.Drawing.Size(1096, 23);
this.button.TabIndex = 0;
this.button.Text = "Add";
this.button.Click += new System.EventHandler(this.ButtonClicked);
//
// chkReadOnly
//
this.chkReadOnly.Dock = System.Windows.Forms.DockStyle.Top;
this.chkReadOnly.Location = new System.Drawing.Point(0, 23);
this.chkReadOnly.Name = "chkReadOnly";
this.chkReadOnly.Size = new System.Drawing.Size(1096, 24);
this.chkReadOnly.TabIndex = 2;
this.chkReadOnly.Text = "ReadOnly";
this.chkReadOnly.CheckedChanged += new
System.EventHandler(this.chkReadOnly_CheckedChanged);
//
// panel1
//
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel1.Location = new System.Drawing.Point(0, 47);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(1096, 703);
this.panel1.TabIndex = 3;
//
// FrmTest
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(1096, 750);
this.Controls.Add(this.panel1);
this.Controls.Add(this.chkReadOnly);
this.Controls.Add(this.button);
this.Name = "FrmTest";
this.Text = "Sample Datagrid with Dynamic row height and word wrap";
this.ResumeLayout(false);

}
#endregion

private void Button2Clicked(object sender, EventArgs e)
{
if(clickcount % 2 == 0)
((DataView)grid.DataSource).RowFilter = "Message = '3 lines\r\n3
lines\r\n3 lines'";
else
((DataView)grid.DataSource).RowFilter = "";
clickcount++;
}

private void ButtonClicked(object sender, EventArgs e)
{
System.IO.StreamReader textReader = new
System.IO.StreamReader(@"election1.txt");

string bigString = textReader.ReadToEnd();
textReader.Close();
table.Rows.Add(new String[]{"Big Row", "Big Row\r\nBig Row\r\nBIG
ROW\r\nBIG ROW\r\nBIG ROW\r\nBIG ROW"});
table.Rows.Add(new String[]{"Med","MEDIUM ROW\r\nMEDIUM ROW"});
table.Rows.Add(new String[]{"Small","SMALL ROW"});
table.Rows.Add(new String[]{"Really Big String",bigString});
table.Rows.Add(new String[]{bigString, "Really Big String"});
}

static void Main()
{
Application.Run(new FrmTest());
}

private void chkReadOnly_CheckedChanged(object sender, System.EventArgs e)
{
grid.ReadOnly = chkReadOnly.Checked;
grid.Invalidate();
}
}
}
//*******************Custom Datagrid********

using System;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;

namespace DynamicColumn
{

[ToolboxItem(true)]
[ToolboxItemFilter("System.Windows.Forms")]
public class AutoSizeDataGrid : System.Windows.Forms.DataGrid
{
#region Class Instances and Properties
/// <summary>
/// The ContextMenu that's shown on a right click.
/// </summary>
private System.Windows.Forms.ContextMenu columnSelectMenu = null;
/// <summary>
/// The 'Show / Hide' MenuItem for the ContextMenu.
/// </summary>
private System.Windows.Forms.MenuItem showHideItem = null;
/// <summary>
/// The 'Reset' MenuItem for the ContextMenu.
/// </summary>
private System.Windows.Forms.MenuItem resetItem = null;
/// <summary>
/// The 'Reset AutoSize' MenuItem for the Reset MenuItem.
/// </summary>
private System.Windows.Forms.MenuItem resetAutoSizeItem = null;
/// <summary>
/// The 'Reset Width' MenuItem for the Reset MenuItem.
/// </summary>
private System.Windows.Forms.MenuItem resetWidthItem = null;
/// <summary>
/// Switch that indicates wether the column width sizing routine is running.
/// </summary>
private bool inAutoSizing = false;
/// <summary>
/// Switch that indicates wether to do autosizing of rows and columns.
/// </summary>
private bool doAutoSizing = true;
/// <summary>
/// Index position for the 'Show / Hide' MenuItem.
/// </summary>
private int IDX_SHOWHIDE = 0;
/// <summary>
/// Index position for the 'Reset' MenuItem.
/// </summary>
private int IDX_RESET = 1;
/// <summary>
/// Index position for the 'Reset AutoSize' MenuItem.
/// </summary>
private int IDX_RESET_AUTOSIZE = 0;
/// <summary>
/// Index position for the 'Reset Width' MenuItem.
/// </summary>
private int IDX_RESET_WIDTH = 1;
/// <summary>
/// Switch that indicates wether to do autosizing of rows and columns.
/// </summary>
[Bindable(true)]
[Category("Auto Sizing")]
[DefaultValue(true)]
[RefreshProperties(RefreshProperties.Repaint)]
[Description("Determines wether the control autosizes it's rows and
columns.")]
public bool DoAutoSizing
{
get{ return doAutoSizing;}
set{ doAutoSizing = value;}
}
#endregion

#region Public Interface
/// <summary>
/// Create a new instance of AutoSizeDataGrid
/// with autosizing enabled.
/// </summary>
public AutoSizeDataGrid() : base()
{
InitializeComponent();
Write("Initializing");
}

/// <summary>
/// Call this method to force the DataGrid to resize it's column widths.
/// </summary>
public void ResizeGrid()
{
AutoSizeDataGrid_SizeChanged(this,new System.EventArgs());
}

/// <summary>
/// Create a DataGridTableStyle with columns that are able to resize
/// it's row heights.
/// </summary>
/// <param name="table">The DataTable to create the DataGridTableStyle
for.</param>
/// <returns>A DataGridTableStyle with autosizing row heights.</returns>
public DataGridTableStyle CreateAutoSizeTableStyle(DataTable table, bool
addToCollection)
{
// Check input
if(table == null)
return null;
else
{
// Create and map the returned style.
DataGridTableStyle style = new DataGridTableStyle();
style.MappingName = table.TableName;
// Add AutoSizeDataGridTextBoxColumns for each DataColumn.
foreach(DataColumn column in table.Columns)
{
// Create the column.
AutoSizeDataGridTextBoxColumn gridColumn = new
AutoSizeDataGridTextBoxColumn();
// Initialize it.
gridColumn.MappingName = column.ColumnName;
gridColumn.HeaderText = column.ColumnName;
gridColumn.Width = this.PreferredColumnWidth;
// We need to be notified about width changes.
gridColumn.WidthChanged += new
System.EventHandler(this.DataGridColumn_WidthChanged);
// Finally add it to the collection.
style.GridColumnStyles.Add(gridColumn);
}
if(addToCollection)
TableStyles.Add(style);
return style;
}
}
#endregion

#region Private Interface
/// <summary>
/// Initializes this instance of AutoSizeDataGrid.
/// This will create the default ContextMenu with its MenuItems,
/// and hook up some events for showing the ContextMenu and detecting
/// size changes.
/// </summary>
private void InitializeComponent()
{
this.columnSelectMenu = new System.Windows.Forms.ContextMenu();
this.showHideItem = new System.Windows.Forms.MenuItem();
this.resetItem = new System.Windows.Forms.MenuItem();
this.resetAutoSizeItem = new System.Windows.Forms.MenuItem();
this.resetWidthItem = new System.Windows.Forms.MenuItem();

((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
//
// columnSelectMenu
//
this.columnSelectMenu.Popup += new
System.EventHandler(this.columnSelectMenu_Popup);
//
// showHideItem
//
this.showHideItem.Text = "Show / Hide";
this.showHideItem.Index = IDX_SHOWHIDE;
this.columnSelectMenu.MenuItems.Add(showHideItem);
//
// resetItem
//
this.resetItem.Text = "Reset";
this.resetItem.Index = IDX_RESET;
this.columnSelectMenu.MenuItems.Add(resetItem);
//
// resetAutoSizeItem
//
this.resetAutoSizeItem.Text = "Auto Width Columns";
this.resetAutoSizeItem.Index = IDX_RESET_AUTOSIZE;
this.resetAutoSizeItem.Click += new
System.EventHandler(this.columnSelectMenu_MenuItem_Click);
this.resetItem.MenuItems.Add(resetAutoSizeItem);
//
// resetWidthItem
//
this.resetWidthItem.Text = "Column Widths";
this.resetWidthItem.Index = IDX_RESET_WIDTH;
this.resetWidthItem.Click += new
System.EventHandler(this.columnSelectMenu_MenuItem_Click);
this.resetItem.MenuItems.Add(resetWidthItem);
//
// AutoSizeDataGrid
//
//this.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.AutoSizeDataGrid_MouseDown);
this.SizeChanged += new
System.EventHandler(this.AutoSizeDataGrid_SizeChanged);
this.DataSourceChanged += new
System.EventHandler(this.AutoSizeDataGrid_DataSourceChanged);
((System.ComponentModel.ISupportInitialize)(this)).EndInit();
}

/// <summary>
/// Returns the DataGridTableStyle that renders the current
/// DataSource. When the current DataSource is a DataTable or
/// DataView the tablename is verified for this, otherwise
/// the DataMember property is queried for the right binding name.
/// </summary>
/// <returns></returns>
private DataGridTableStyle GetCurrentTableStyle()
{
// no source, no style
if(DataSource == null)
return null;
// For tables or views, get the tablename
if(DataSource is DataTable)
return TableStyles[((DataTable)DataSource).TableName];
else if(DataSource is DataView && ((DataView)DataSource).Table != null)
return TableStyles[((DataView)DataSource).Table.TableName];
// otherwise the DataMember property
else
return TableStyles[DataMember];
}

/// <summary>
/// Gets called when the DataGrid's DataSource changes. Resize the grid.
/// </summary>
/// </summary>
/// <param name="sender">The object that raised the event.</param>
/// <param name="e">The arguments belonging to the event.</param>
private void AutoSizeDataGrid_DataSourceChanged(object sender,
System.EventArgs e)
{
AutoSizeDataGrid_SizeChanged(sender, e);
}
/// <summary>
/// Gets called when the size of the DataGrid changes. Resize the
/// columns when the user wants to.
/// </summary>
/// <param name="sender">The object that raised the event.</param>
/// <param name="e">The arguments belonging to the event.</param>
private void AutoSizeDataGrid_SizeChanged(object sender, System.EventArgs e)
{
Write("DataGrid size change detected, autosizing grid.");
// Does the user want to autosize.
if(DoAutoSizing)
{
// Indicate we're autosizing now.
inAutoSizing = true;
// Get current style that needs to be sized
DataGridTableStyle currentStyle = GetCurrentTableStyle();
// Is it available and does it have columns to resize?
if(currentStyle != null && currentStyle.GridColumnStyles.Count > 0)
{
// Calculate the increment per column,
// based on the difference between the actual grid with and
// the clientsize.width. After resizing, this will leave a little
// bit for the last column.
int gridWidth = GetGridWidth();
if(gridWidth == -1)
return;
int componentWidth = this.ClientSize.Width;

int diff = componentWidth - gridWidth;
int colCount = currentStyle.GridColumnStyles.Count;
int incPerColumn = (int)diff / colCount;

// Resize all columns that are available.
Write("NrColumns: " + colCount + " TotalSizeDifference: " + diff + "
Increment: " + incPerColumn + " Left over: " + ( diff -
(incPerColumn*colCount)));
foreach(DataGridColumnStyle column in currentStyle.GridColumnStyles)
{
// If the column is a AutoSizeDataGridTextBoxColumn, and the user
// has resized it him/herself, do not autosize it.
// Also don't autosize hidden columns(width = 0)
if(column is AutoSizeDataGridTextBoxColumn &&
((AutoSizeDataGridTextBoxColumn)column).OverrideAutoWidth == true ||
column.Width == 0)
{
Write("Skipping column " + column.HeaderText);
continue;
}
Write("Adding " + incPerColumn + " to column " + column.HeaderText);
column.Width += incPerColumn;
// Do not autosize the column smaller than the PreferredColumnWidth
if(column.Width < PreferredColumnWidth)
column.Width = PreferredColumnWidth;
// for calculating the left over part.
diff -= incPerColumn;
//((AutoSizeDataGridTextBoxColumn)column).TextBox.WordWrap = true;


}
// Add the left over part to the last visible column.
Write("Adding left over items to last visible column, " + diff + "
pixels");
for(int i = colCount - 1; i >= 0; i--)
if(currentStyle.GridColumnStyles[i].Width > 0)
{
currentStyle.GridColumnStyles[i].Width += diff;
break;
}
}
// Done autosizing
inAutoSizing = false;
}
}

/// <summary>
/// Gets called when the user clicks the grid. If the right
/// button was clicked, show the ContextMenu.
/// </summary>
/// <param name="sender">The object that raised the event.</param>
/// <param name="e">The arguments belonging to the event.</param>
// private void AutoSizeDataGrid_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
// {
// // If right mouse button clicked
// if(e.Button == System.Windows.Forms.MouseButtons.Right)
// // show context menu
// this.ContextMenu = columnSelectMenu;
// else
// // Remove the context menu
// this.ContextMenu = null;
// }

/// <summary>
/// Gets called when a column in the current DataGridTableStyle
/// changes width. When we're not autosizing, set the override
/// flag on the column. When the new width is 0, reset the override
/// flag to false.
/// </summary>
/// <param name="sender">The object that raised the event.</param>
/// <param name="e">The arguments belonging to the event.</param>
private void DataGridColumn_WidthChanged(object sender, System.EventArgs e)
{
// Are we autosizing
if(!inAutoSizing)
{
// If the column is visible set the override flag.
if(((AutoSizeDataGridTextBoxColumn)sender).Width != 0)
((AutoSizeDataGridTextBoxColumn)sender).OverrideAutoWidth = true;
else
// otherwise don't
((AutoSizeDataGridTextBoxColumn)sender).OverrideAutoWidth = false;
// Resize because the last column doesn't fit now.
ResizeGrid();
}
}

/// <summary>
/// Gets called when the ContextMenu pops up. Rebuild the menu
/// with the right checkmarks for hidden columns.
/// </summary>
/// <param name="sender">The object that raised the event.</param>
/// <param name="e">The arguments belonging to the event.</param>
private void columnSelectMenu_Popup(object sender, System.EventArgs e)
{
// Remove old items
this.showHideItem.MenuItems.Clear();
// Get the current style
DataGridTableStyle currentStyle = GetCurrentTableStyle();
// No style, no play
if(currentStyle != null)
{
// Create a MenuItem for each DataGridColumnStyle.
foreach(System.Windows.Forms.DataGridColumnStyle columnStyle in
currentStyle.GridColumnStyles)
{
// Create the MenuItem
MenuItem item = new MenuItem();
// Set it's properties
item.Text = columnStyle.MappingName;
item.Checked = columnStyle.Width == 0 ? false : true;
// Connect to event
item.Click += new
System.EventHandler(this.columnSelectMenu_MenuItem_Click);
// Finally add it.
this.showHideItem.MenuItems.Add(item);
}
}
}

/// <summary>
/// Gets called when a MenuItem in the ContextMenu is clicked.
/// Verify which item it was and take action accordingly.
/// </summary>
/// <param name="sender">The object that raised the event.</param>
/// <param name="e">The arguments belonging to the event.</param>
private void columnSelectMenu_MenuItem_Click(object sender,
System.EventArgs e)
{
// Indicate we're autosizing now.
inAutoSizing = true;
try
{
// Obtain the clicked item
MenuItem menuItem = (MenuItem)sender;
// Get the current style
DataGridTableStyle currentStyle = GetCurrentTableStyle();
// No style, no play
if(currentStyle != null)
{
// Which item was clicked?
if(menuItem.Parent == showHideItem)
// An item in the showHideItem MenuItem
ShowHideColumn(currentStyle,menuItem);
else if (menuItem.Parent == resetItem)
{
// An item in the resetItem MenuItem
// Verify the index to know which one
if(menuItem.Index == IDX_RESET_AUTOSIZE)
ResetAutoSizeOverride(currentStyle);
else if (menuItem.Index == IDX_RESET_WIDTH)
ResetWidth(currentStyle);
}
}
}
catch{}
// Done autosizing
inAutoSizing = false;
}

/// <summary>
/// Gets called when an item in the ShowHide MenuItem is clicked.
/// Show or hide the column.
/// </summary>
/// <param name="currentStyle">The DataGridTableStyle for the
/// currently displayed DataSource.</param>
/// <param name="menuItem">The MenuItem that was clicked.</param>
private void ShowHideColumn(DataGridTableStyle currentStyle, MenuItem
menuItem)
{
// Get the column to show or hide
DataGridColumnStyle columnStyle =
currentStyle.GridColumnStyles[menuItem.Text];
// What to do, show or hide?
if(!menuItem.Checked)
{
/* Show */
// reset to default width
columnStyle.Width = this.PreferredColumnWidth;
// Is the column the last visible?
int styleIndex = currentStyle.GridColumnStyles.IndexOf(columnStyle);
bool lastVisible = true;
// If we're not the last column, are we the last visible one?
if(styleIndex < currentStyle.GridColumnStyles.Count - 1)
// Run through all next columns
for(int i = styleIndex + 1; i < currentStyle.GridColumnStyles.Count;
i++)
if(currentStyle.GridColumnStyles[i].Width > 0)
lastVisible = false; // nope not last visible
// When we are last visible, resize the prior visible one to default size
if(lastVisible && styleIndex > 0)
for(int i = styleIndex - 1; i >= 0; i--)
if(currentStyle.GridColumnStyles[i].Width > 0)
{
currentStyle.GridColumnStyles[i].Width = this.PreferredColumnWidth;
break;
}
}
else
/* Hide */
columnStyle.Width = 0;
// Reflect changes for the menuitem
menuItem.Checked = !menuItem.Checked;
// Resize the grid
ResizeGrid();
this.Invalidate();
}

/// <summary>
/// Gets called when the ResetAutoSize item is clicked.
/// Set OverrideAutoWidth for currentStyle to false.
/// </summary>
/// <param name="currentStyle">The DataGridTableStyle for the
/// currently displayed DataSource.</param>
private void ResetAutoSizeOverride(DataGridTableStyle currentStyle)
{
if(currentStyle != null)
foreach(AutoSizeDataGridTextBoxColumn column in
currentStyle.GridColumnStyles)
{
column.OverrideAutoWidth = false;
}
}

/// <summary>
/// Gets called when the ResetWidth item is clicked.
/// Set Width for all columns to PreferredColumnWidth
/// and resize the grid.
/// </summary>
/// <param name="currentStyle">The DataGridTableStyle for the
/// currently displayed DataSource.</param>
private void ResetWidth(DataGridTableStyle currentStyle)
{
if(currentStyle != null)
foreach(AutoSizeDataGridTextBoxColumn column in
currentStyle.GridColumnStyles)
{
column.Width = PreferredColumnWidth;
column.TextBox.WordWrap = true;
}
ResizeGrid();
}

/// <summary>
/// Returns the actual width of the grid in pixels.
/// </summary>
/// <returns>The actual gridwidth in pixels.</returns>
private int GetGridWidth()
{
// The DataGridTableStyle columns contain the requested information
DataGridTableStyle currentStyle = GetCurrentTableStyle();
if(currentStyle != null)
{
// Add width for each column
int width = 0;
foreach(DataGridColumnStyle columnStyle in currentStyle.GridColumnStyles)
{
width += columnStyle.Width;
}
// Add width for the first uneditable column
// by verifying the X position of the first cell.
try
{
width += this.GetCellBounds(0,0).X + 2;
}
catch
{
// First cell not available, make an educated guess.
width += 39;
}
// Take scrollbar into account
if(VertScrollBar.Visible == true)
width += VertScrollBar.Width;
return width;
}
// Errorrr
return -1;
}
#endregion

#region Debug
// Used for debug purposes.
[Conditional("GRID_DEBUG")]
internal static void Write(string s)
{
Console.WriteLine(s);
}
#endregion
}
}

//***************Custom Column*****************

using System;
using System.Data;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Reflection;
using System.Diagnostics;
using System.Windows.Forms;
using System.Threading;

namespace DynamicColumn
{

public class AutoSizeDataGridTextBoxColumn: DataGridTextBoxColumn
{
#region Class Instances and Properties
/// <summary>
/// Remember how many times the GetMinimumHeight method is called.
/// </summary>
private int currentIteration = 0;
/// <summary>
/// Flag to indicate wether user has resized the row, overriding the
/// autosize width behaviour.
/// </summary>
private bool overrideAutoWidth = false;
/// <summary>
/// Flag to indicate wether user has resized the row, overriding the
/// autosize width behaviour.
/// </summary>
public bool OverrideAutoWidth
{
get
{
return overrideAutoWidth;
}
set
{
overrideAutoWidth = value;
}
}

#endregion

#region Public Interface
/// <summary>
/// Create a new instance of AutoSizeDataGridTextBoxColumn.
/// </summary>
public AutoSizeDataGridTextBoxColumn() : base(){}

/// <summary>
/// Reset the iteration count to 0.
/// </summary>
public void ResetIterations()
{
AutoSizeDataGrid.Write("Resetting column iteration count.");
currentIteration = 0;
}

protected override void Paint(System.Drawing.Graphics g,
System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source,
int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush,
bool alignToRight)
{
base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
g.FillRectangle(backBrush, bounds);
string vText = ((string)(base.GetColumnValueAtRow(source, rowNum)));
g.DrawString(vText, this.TextBox.Font, foreBrush, new
RectangleF(bounds.X, bounds.Y, bounds.Width, bounds.Height));
}

#endregion

#region Row Height Calculation
/// <summary>
/// The actual work is done in this method.
/// - First verify if there are rows, find out from CurrencyManager
/// - increment iteration count
/// - calculate height of cell at iterationCount - 1
/// - if iterationCount is rowcount, reset the iterationcount
/// - return
/// </summary>
/// <returns>The height of the requested row.</returns>
protected override int GetMinimumHeight()
{
try
{
StackFrame frame = new StackFrame(4);
MethodBase method = frame.GetMethod();
string s = method.DeclaringType.FullName;
if(s.EndsWith("DataGridAddNewRow"))
{
this.ResetIterations();
return base.GetMinimumHeight();
}
else{

CurrencyManager cur =
(CurrencyManager)this.DataGridTableStyle.DataGrid.BindingContext
[this.DataGridTableStyle.DataGrid.DataSource,this.DataGridTableStyle.DataGrid.DataMember];
if(cur == null || cur.Count == 0)
return base.GetMinimumHeight();

this.currentIteration++;

int retVal = base.GetMinimumHeight();
retVal = this.CalcStringHeight(GetColumnValueAtRow(cur,currentIteration
- 1).ToString());
if(currentIteration == cur.Count)
this.ResetIterations();
return retVal;
}
}
catch
{
AutoSizeDataGrid.Write("Error occured, returning default.");
// Error, return default
return base.GetMinimumHeight();
}
}

/// <summary>
/// Calculates the height of the supplied string.
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
private int CalcStringHeight(string s)
{
try
{
AutoSizeDataGrid.Write("Measuring height of string value.");
// Create graphics for calculation
System.Drawing.Graphics g = this.TextBox.CreateGraphics();
// Do measure, and add a bit (4 pixels for me)
return (int)g.MeasureString(s,this.TextBox.Font).Height + 4;
}
catch
{
AutoSizeDataGrid.Write("Error occured while measuring height of string
value, returning default.");
// Error, return default font height.
return base.GetMinimumHeight();
}
}
#endregion
}
}
//********Sample large text file for column (election1.txt)***********
Election to
Amortize Organizational Expenditures
Pursuant to I.R.C. Section 248



XYZ, Inc.
EIN: 52-1234567
Taxable Year Ended December 31, 2004

Pursuant to §248 of the Internal Revenue Code, XYZ, Inc. hereby elects to
deduct $5,000 of its organizational expenditures currently, and deduct the
remainder of the organizational expenditures ratably over a period of 180
months starting October 2004, the month in which it began business. In
accordance with Regs. §1.248-1(c), the following information is submitted:

Description of Expenditures Amount When Incurred
--------------------------- ------ -------------
State incorporation fees $ 4,900 November 2004

Postage, telephone, etc. 350 December 2004
------
5,250
======
  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off