TextBoxColumn FormatString in Editors

7 posts, 0 answers
  1. Rachel
    Rachel avatar
    37 posts
    Member since:
    Mar 2008

    Posted 02 Sep 2009 Link to this post

    Hello,
    I am using a GridViewTextBoxColumn to display decimal values, and I am applying a formatstring like "{0:0.00##########}" to the column to trim off what may be several trailing zeros in the data.  The data is formatted properly and looks great in the column, but when I go to edit the data by double-clicking, the formatstring is no longer applied.  So my edit box is filled up with the trailing zeros, making editing cumbersome to the user.  Is there a way I can apply my formatstring to what shows up in the editor as well?

    Thank you,
    Rachel
    Rachel
  2. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 03 Sep 2009 Link to this post

    Hello Rachel,

    You can use CellEditorInitialized event to apply formatting to the textbox editor in RadGridView. Please consider the code snippet below:

    void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e) 
        RadTextBoxEditor editor = this.radGridView1.ActiveEditor as RadTextBoxEditor; 
        if (editor != null && this.radGridView1.CurrentColumn.HeaderText == "Value"
        { 
            editor.Value = string.Format("{0:0.00##########}"this.radGridView1.CurrentCell.Value); 
        } 

    If you need further assistance, please write me back.

    Best wishes,
    Jack
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. SHAHROKH
    SHAHROKH avatar
    8 posts
    Member since:
    Oct 2012

    Posted 22 Aug 2012 Link to this post

    Hello guys.
    I have a RadGridViewTextbox column and i want that user enter just text values in this column.How can i do that?
    Users not allow to enter numeric values in this column.
  5. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 24 Aug 2012 Link to this post

    Hi Shahrokh,

    You have two options to solve this issue:

    1. If you want to enter a text with a limited length, you can use the GridViewMaskBoxColumn. Consider the following code:
    GridViewMaskBoxColumn maskedBoxCol = new GridViewMaskBoxColumn("Aa");
    maskedBoxCol.MaskType = MaskType.Standard;
    maskedBoxCol.Mask = "LLLLLLL";           
    this.radGridView1.Columns.Add(maskedBoxCol);

    2. When the user should be able to enter a text with unlimited length, you have to handle the KeyPress event for the text box editor:
    RadTextBoxEditor lastTextBoxEditor;
     
    void radGridView1_CellEndEdit(object sender, GridViewCellEventArgs e)
    {
        if (lastTextBoxEditor != null)
        {
            RadTextBoxEditorElement editorElement = (RadTextBoxEditorElement)lastTextBoxEditor.EditorElement;
            editorElement.TextBoxItem.HostedControl.KeyPress -= new KeyPressEventHandler(HostedControl_KeyPress);
            lastTextBoxEditor = null;
        }
    }
     
    void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
    {
        RadTextBoxEditor textBoxEditor = e.ActiveEditor as RadTextBoxEditor;
        if (textBoxEditor != null)
        {
            lastTextBoxEditor = textBoxEditor;
            RadTextBoxEditorElement editorElement = (RadTextBoxEditorElement)textBoxEditor.EditorElement;
            editorElement.TextBoxItem.HostedControl.KeyPress += new KeyPressEventHandler(HostedControl_KeyPress);
        }
    }
     
    void HostedControl_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar < 65 || e.KeyChar > 122)
        {
            e.Handled = true;
        }
    }

    I hope this helps.
     
    All the best,
    Jack
    the Telerik team
    RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
  6. SHAHROKH
    SHAHROKH avatar
    8 posts
    Member since:
    Oct 2012

    Posted 25 Aug 2012 Link to this post

    Hi Jack.
    Thanks.This was helpful.
    Another problem:
    I have grid with template.How can i put value of  cell in template into textbox?
  7. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 29 Aug 2012 Link to this post

    Hello Shahrokh,

    If I understand correctly, you want to get the cell value. If this is true, then you should use the Cells collection of the row. You can address a cell by its index or by its name. For example:
    int cellValue = (int)this.radGridView1.Rows[2].Cells["Name"].Value;

    I hope this helps.
     
    Kind regards,
    Jack
    the Telerik team
    RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
  8. SHAHROKH
    SHAHROKH avatar
    8 posts
    Member since:
    Oct 2012

    Posted 31 Aug 2012 Link to this post

    Hello Jack.
    Thanks for your reply.thank you for your answer.
    Regards,
    Shahrokh.
Back to Top
UI for WinForms is Visual Studio 2017 Ready