Changes to datagrid text are lost in Visual C# .NET 2003 project (839225)
The information in this article applies to:
- Microsoft Visual C# .NET (2003)
VSWhidbey:95474Content Maintenance:15226 SYMPTOMSWhen you partially select and modify column data in a datagrid in a Microsoft Visual C# .NET 2003 project, the changes may be lost when you move to a new row in the datagrid.
If you select and edit all the data in the column, the changes are retained when you move to the new row.CAUSEThis issue occurs if the data is formatted in a custom numeric format such as currency or in a format that includes the comma symbol.WORKAROUNDTo work around this issue in Visual C# .NET 2003, override the PropertyDescriptor class to convert the text to a format that the base implementation can parse. After you determine the correct parse method, use this method in the Commit method: //Visual C# .NET
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Globalization;
using System.Reflection;
namespace DataGrid_example
{
/// <summary>
/// Class that is used when column text is required in a custom numeric format
/// such as a comma, or a currency. This class overloads the Commit method
/// of DataGridTextBoxColumn.
///
/// Refers to <Main Form Code> private DataGrid_example.FormattingDataGridTextBoxColumn dataGridTextBoxColumn3; </Main form code>
/// </summary>
public class FormattingDataGridTextBoxColumn : DataGridTextBoxColumn
{
//Return object form Parse method.
private MethodInfo localParseMethod;
//Default constructor for class.
public FormattingDataGridTextBoxColumn() : base()
{
}
//Default overloaded constructor for class.
public FormattingDataGridTextBoxColumn( PropertyDescriptor prop, string format, bool isDefault) : base(prop, format, isDefault)
{
}
//Overiding the PropertyDescriptor class to covert text to
//a format the base implementation can parse.
public override PropertyDescriptor PropertyDescriptor
{
set
{
//Set the base value equal to the passed in value.
base.PropertyDescriptor = value;
this.localParseMethod = null;
if(this.PropertyDescriptor != null)
{
if( this.PropertyDescriptor.PropertyType != null)
{
//Set method info for
this.localParseMethod = this.PropertyDescriptor.PropertyType.GetMethod("Parse", new Type[]{typeof(string), typeof(NumberStyles), typeof(IFormatProvider)});
}
}
}
}
protected override bool Commit( CurrencyManager dataSource, int rowNum)
{
if( null != this.localParseMethod )
{
try
{
//Get the current value of the cell.
object value = this.TextBox.Text;
if(! NullText.Equals(value))
{
//Get the new value and format it as a number.
value = this.localParseMethod.Invoke( null, new object[] { value, NumberStyles.Any, this.FormatInfo});
//Set the value in the grid back to the raw number.
this.TextBox.Text = value.ToString();
}
}
catch( Exception ){ }
}
//update the dataset
return base.Commit( dataSource, rowNum);
}
}
}
STATUS
Microsoft has confirmed that this is a bug in Microsoft C# .NET 2003.
Modification Type: | Minor | Last Reviewed: | 1/12/2006 |
---|
Keywords: | kbprb KB839225 kbAudDeveloper |
---|
|