Microsoft KB Archive/839225

= Changes to datagrid text are lost in Visual C# .NET 2003 project =

Article ID: 839225

Article Last Modified on 1/12/2006

-

APPLIES TO


 * Microsoft Visual C# .NET 2003 Standard Edition

-





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



CAUSE
This issue occurs if the data is formatted in a custom numeric format such as currency or in a format that includes the comma symbol.



WORKAROUND
To 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 {   ///     ///  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  private DataGrid_example.FormattingDataGridTextBoxColumn dataGridTextBoxColumn3;  ///    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(&quot;Parse&quot;, 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.

Keywords: kbprb KB839225

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.