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 |
Published Date: February 18, 2015 |
Last Revised Date: June 24, 2021
Tags: formatting, cell

Solution

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.

C#

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);
}

 

VB

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.

C#

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);
 }

 

VB

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.

 

C#

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.Style.BeginUpdate();
       e.Text = GridCellValueConvert.FormatValue(value, e.Style.CellValueType, e.Style.Format, ci,   nfi);
       e.Style.CellValue = value;
       e.Style.EndUpdate();
       e.Handled = true;
      }
     else
      {
        MessageBox.Show(e.Text + " is invalid value!", "Invalid Type/ Format");
        e.Handled = true;
        if (this.gridControl1.CurrentCell != null)
        this.gridControl1.CurrentCell.CancelEdit();
        //SendKeys.Send("{Esc}");
       }
     }
}

 

VB

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.Style.BeginUpdate()
        e.Text = GridCellValueConvert.FormatValue(value, e.Style.CellValueType, e.Style.Format, ci, nfi)
        e.Style.CellValue = value
        e.Style.EndUpdate()
        e.Handled = True
     Else
        MessageBox.Show(e.Text & " is invalid value!", "Invalid Type/ Format")
        e.Handled = True
        If Me.gridControl1.CurrentCell IsNot Nothing Then
            Me.gridControl1.CurrentCell.CancelEdit()
        End If
    'SendKeys.Send("{Esc}");
     End If
  End If
End Sub

 

Sample

 

https://www.syncfusion.com/downloads/support/directtrac/general/ze/WF-15698_How_to_mimic_Excel_like_behavior_for_cells_that_has_Format_string1979433900540258780.zip

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
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