We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to mimic the Excel-like behavior for the cells that have the Format string?

Platform: WinForms |
Control: GridAwareTextBox
Tags: formatting, cell


In Excel, when the current cell enters into the edit mode it has the Value string and not the formatted string. The Excel formula bar always displays the Value String and not the formatted string. These behaviors can be achieved in the GridControl by installing a handler for CurrentCellInitializeControlText event and by setting the ControlText to Value string instead of the formatted string. This sets the GridAwareTextBox with value string consistently.

The following code example is for handling the CurrentCellInitializeText event.


this.gridControl1.CurrentCellInitializeControlText += new Syncfusion.Windows.Forms.Grid.GridCurrentCellInitializeControlTextEventHandler(gridControl1_CurrentCellInitializeControlText);
void gridControl1_CurrentCellInitializeControlText(object sender, Syncfusion.Windows.Forms.Grid.GridCurrentCellInitializeControlTextEventArgs e)
 //initialize the control text with value string instead of formatted string.            e.ControlText = e.Style.GetText(e.CellValue);



AddHandler gridControl1.CurrentCellInitializeControlText, AddressOf gridControl1_CurrentCellInitializeControlText
Private Sub gridControl1_CurrentCellInitializeControlText(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Grid.GridCurrentCellInitializeControlTextEventArgs)
  'initialize the control text with value string instead of formatted string.
  e.ControlText = e.Style.GetText(e.CellValue)
End Sub


Other different behaviors and options

GridAwareTextBox always with FormattedText

The GridAwareTextBox is implemented by following a consistent pattern and it always displays the .Text. When the CurrentCell is activated and GridCurrentCell is changed, the GridAwareTextBox text is set with the CurrentCell.Renderer.ControlText that is the formatted string, since by default the ControlText is initialized with the formatted text. So when you press Escape key while editing a cell, the GridAwareTextBox is again set with .Text that is not the formatted text. When you want to display the FormattedText consistently in the GridAwareTextBox then you can create sub class for the GridAwareTextBox, override the GridCurrentCellRejectedChanges and set the .FormattedText instead of the .Text

The following code example is for GridCurrentCellRejectedChanges event.


protected override void GridCurrentCellRejectedChanges(object sender, EventArgs e)
   GridControlBase grid = sender as GridControlBase;
   GridCurrentCell cc = grid.CurrentCell;
               //Always display FormattedText
   SetText(grid.Model[cc.RowIndex, cc.ColIndex].FormattedText);//.Text);



Protected Overrides Sub GridCurrentCellRejectedChanges(ByVal sender As Object, ByVal e As EventArgs)
   Dim grid As GridControlBase = TryCast(sender, GridControlBase)
   Dim cc As GridCurrentCell = grid.CurrentCell
  'Always display FormattedText
  SetText(grid.Model(cc.RowIndex, cc.ColIndex).FormattedText) '.Text);
End Sub

Parsing Formats

To save the underlying cell value from the formatted text, the ParseCommonFormats / SaveCellFormattedText event can be handled. The attached sample populates Text with formats when you miss any format character and also parses the formatted string and saves the underlying cell value, in the ParseCommonFormats event handler.



this.gridControl1.ParseCommonFormats += new GridCellTextEventHandler(gridControl1_ParseCommonFormats);
void gridControl1_ParseCommonFormats(object sender, GridCellTextEventArgs e)
   if (e.Style.CellIdentity.ColIndex == 1 && !this.checkBox1.Checked)
     //populate the formats if user had missed by chance 
     //eg. if the user missed any one bracket. Entered Cell Value: "(45.00" .
     string text = e.Text;
     text = text.Replace("(", "");
     text = text.Replace(")", "");
     Decimal value;
     if (Decimal.TryParse(text, out value))
       System.Globalization.CultureInfo ci = e.Style.CultureInfo;
       System.Globalization.NumberFormatInfo nfi = ci != null ? ci.NumberFormat : null;
       e.Text = GridCellValueConvert.FormatValue(value, e.Style.CellValueType, e.Style.Format, ci,   nfi);
       e.Style.CellValue = value;
       e.Handled = true;
        MessageBox.Show(e.Text + " is invalid value!", "Invalid Type/ Format");
        e.Handled = true;
        if (this.gridControl1.CurrentCell != null)



Private Sub gridControl1_ParseCommonFormats(ByVal sender As Object, ByVal e As GridCellTextEventArgs)
  If e.Style.CellIdentity.ColIndex = 1 AndAlso (Not Me.checkBox1.Checked) Then
     'populate the formats if user had missed by chance 
     'eg. if the user missed any one bracket. Entered Cell Value: "(45.00" .
     Dim text As String = e.Text
     text = text.Replace("(", "")
     text = text.Replace(")", "")
     Dim value As Decimal
     If Decimal.TryParse(text, value) Then
        Dim ci As System.Globalization.CultureInfo = e.Style.CultureInfo
        Dim nfi As System.Globalization.NumberFormatInfo = If(ci IsNot Nothing, ci.NumberFormat, Nothing)
        e.Text = GridCellValueConvert.FormatValue(value, e.Style.CellValueType, e.Style.Format, ci, nfi)
        e.Style.CellValue = value
        e.Handled = True
        MessageBox.Show(e.Text & " is invalid value!", "Invalid Type/ Format")
        e.Handled = True
        If Me.gridControl1.CurrentCell IsNot Nothing Then
        End If
     End If
  End If
End Sub





2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile