This is a migrated thread and some comments may be shown as answers.

TextBoxColumn FormatString in Editors

6 Answers 405 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Rachel
Top achievements
Rank 2
Rachel asked on 03 Sep 2009, 01:17 AM
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

6 Answers, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 03 Sep 2009, 06:47 AM
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.
0
SHAHROKH
Top achievements
Rank 2
answered on 22 Aug 2012, 08:49 AM
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.
0
Jack
Telerik team
answered on 24 Aug 2012, 02:52 PM
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 >>
0
SHAHROKH
Top achievements
Rank 2
answered on 25 Aug 2012, 12:29 PM
Hi Jack.
Thanks.This was helpful.
Another problem:
I have grid with template.How can i put value of  cell in template into textbox?
0
Jack
Telerik team
answered on 29 Aug 2012, 01:35 PM
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 >>
0
SHAHROKH
Top achievements
Rank 2
answered on 31 Aug 2012, 06:00 AM
Hello Jack.
Thanks for your reply.thank you for your answer.
Regards,
Shahrokh.
Tags
GridView
Asked by
Rachel
Top achievements
Rank 2
Answers by
Jack
Telerik team
SHAHROKH
Top achievements
Rank 2
Share this question
or