Geeks With Blogs
Denis Pitcher Denis Pitcher's Tech Blog

I've been stuck for a while now on a problem where I'm trying to format columns in a datagrid using dynamic column styles (datagridcolumnstyles).

The issue was when I was using the DataGridTextBoxColumn.Format property with a value of “c00“ for formatting the column to display a $ at the beginning of numbers.  It was odd because the first 4 columns would format properly and the rest would revert to no formatting.  The odd thing was that the column styles were being set and I was even able to confirm that the format property was set to “c00“, it just wasn't being picked up.

I got fed up searching for a reason why in my own code that I could not find after stepping line by line through it in the debugger numerous times.

In the end I decided to extend the class and create my own to call rather then bothering to use the .format property at all.

source: http://support.microsoft.com/default.aspx?scid=kb;en-us;318581

Code: 

public class MoneyDataGridTextBoxColumn : DataGridTextBoxColumn

{

protected override object GetColumnValueAtRow( CurrencyManager cm, int RowNum )

{

// Get data from the underlying record and format for display.

//

object oVal = base.GetColumnValueAtRow( cm, RowNum );

if ( oVal == System.DBNull.Value )

return NullText;

else

{

try

{

decimal temp = Convert.ToDecimal(oVal); //(decimal)oVal;

return "$" + temp.ToString("0.00");

}

catch( Exception )

{

return NullText;

}

}

}

protected override bool Commit( CurrencyManager cm, int RowNum )

{

// Parse the data and write to underlying record.

//

HideEditBox();

DataGridTextBox box = (DataGridTextBox)TextBox;

decimal Value;

// Do not write data if not editing.

if ( box.IsInEditOrNavigateMode )

return true;

// Detect the null value string.

if ( TextBox.Text == NullText )

SetColumnValueAtRow(cm, RowNum, DBNull.Value);

else

{

try

{

if ( TextBox.Text.StartsWith( "$" ) )

Value = decimal.Parse( TextBox.Text.Substring( 0, TextBox.Text.Length - 1 ) );

else

Value = decimal.Parse( TextBox.Text );

// Write new value.

SetColumnValueAtRow(cm, RowNum, Value);

}

catch( Exception )

{

// Exit on error and display old "good" value.

return false;

}

}

// Let the DataGrid know that processing is completed.

this.EndEdit();

return true;

}

}

 

Keywords: DataGridColumnStyle, Format, DataGridTextBoxColumn, Bug

Posted on Wednesday, February 22, 2006 5:05 PM | Back to top


Comments on this post: DataGridTextBoxColumn Formatting Bug?

# re: DataGridTextBoxColumn Formatting Bug?
Requesting Gravatar...
nice bug resolving. thanks
Left by meters to feet on May 21, 2009 5:58 AM

Your comment:
 (will show your gravatar)


Copyright © Denis Pitcher | Powered by: GeeksWithBlogs.net