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

[Solved] DataFormatString in edit mode

6 Answers 818 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Henrik
Top achievements
Rank 1
Henrik asked on 20 Jan 2009, 09:59 AM
Hi,

I am initially loading the grid in edit mode - and when doing that, RadGrid seems to ignore the value for the DataFormatString property of my GridBoundColumn
It works if I do not load the grid in edit mode.

How can I make it work when loading in edit mode?

6 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 20 Jan 2009, 11:47 AM
Hello Henrik,

DataFormatString is not applied for the controls generated for Edit/Insert mode (they don't have such property).

It's possible to access the TextBox control and format the value as shown below.
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    if (e.Item is GridEditableItem && e.Item.IsInEditMode) 
    { 
        TextBox textBox = ((GridEditableItem)e.Item)["myBoundColumnName"].Controls[0] as TextBox; 
        //apply your custom formatting here 
    } 

Depending on your requirement you may also use GridNumericColumn, GridCheckBoxColumn and GridDateTimeColumn.
Column types

Let us know if you need further assistance

Best regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Henrik
Top achievements
Rank 1
answered on 20 Jan 2009, 12:03 PM
Hi Daniel,

Thanks a lot for your reply. It seems like the NumericColumn suits my needs.
Best regars

Henrik
0
RichElliott
Top achievements
Rank 1
answered on 20 May 2009, 06:00 PM

Refering to your sample code, above, could you give a little more detail on how you would "Apply custom formatting here".  For example, if the GridBoundColumn should only be number with two digits after the decimal, (i.e. 123.45) how would you do it?

 

GridNumbericColumns are too slow for what I'm doing.

 

Thanks,

 

Rich

 

0
Daniel
Telerik team
answered on 25 May 2009, 04:02 PM
Hello Richard,

I meant that it's possible to apply the same DataFormatString to the value in the TextBox.
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    if (e.Item is GridEditableItem && e.Item.IsInEditMode) 
    { 
        TextBox tb = (e.Item as GridEditableItem)["Value"].Controls[0] as TextBox; 
        string dataFormatString = (RadGrid1.MasterTableView.GetColumn("Value"as GridBoundColumn).DataFormatString; 
        tb.Text = String.Format(dataFormatString, double.Parse(tb.Text)); 
    } 

<telerik:GridBoundColumn DataField="Value" HeaderText="Value" UniqueName="Value" 
    DataFormatString="{0:N2}" /> 

Best regards,
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Celeste
Top achievements
Rank 1
answered on 05 Oct 2009, 04:25 PM
Has this changed with latest internal build?
0
Daniel
Telerik team
answered on 08 Oct 2009, 03:29 PM
Hello Celeste,

We don't have plans to change this functionality because the DataFormatString property might decrease the precision of your numeric values. For example if you have 3.43553 and DataFormatString="{0:0.0}" when you press Update, the value will become 3.4. To avoid such problems we recommend you use GridNumericColumn.

Best regards,
Daniel
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.
Tags
Grid
Asked by
Henrik
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Henrik
Top achievements
Rank 1
RichElliott
Top achievements
Rank 1
Celeste
Top achievements
Rank 1
Share this question
or