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

With RadGrid-How to format dates when in edit mode

5 Answers 226 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mark Breen
Top achievements
Rank 1
Mark Breen asked on 22 Mar 2010, 04:34 PM
Hello All,

Using RadGrid with 2009.3.1103.35

I have successfully changed the format of displayed column using the format {0:dd/MM/yyyy}  this works well for view mode.  However, when i switch to edit mode, the format reverts to mm/dd/yyyy HH:MM:SS  format.

How can I set the format for both View and edit Mode?  I would prefer not to have to use form templates as this form contains approx 40 columns.

here is the current code for the column I refer to

        <telerik:GridBoundColumn DataField="DateOfBirth" DataType="System.DateTime"  
            Visible = "True" HeaderText="DateOfBirth" SortExpression="DateOfBirth"  
            UniqueName="DateOfBirth" EditFormColumnIndex="1"  
            DataFormatString="{0:dd/MM/yyyy}"
        </telerik:GridBoundColumn> 
 


Thanks
Mark

5 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 23 Mar 2010, 06:02 AM

Hello Mark,

You can set the format (in EditMode) from code behind as shown below.

C#:

 
    protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)  
    {  
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)  
        {  
            GridEditableItem editItem = (GridEditableItem)e.Item;  
            TextBox textBox = (TextBox)editItem["DateOfBirth"].Controls[0];  
            textBox.Text = Convert.ToDateTime(textBox.Text).ToString("d");   
        }  
    } 

-Shinu.

0
Mark Breen
Top achievements
Rank 1
answered on 28 Mar 2010, 12:25 PM
Hello Shinu,

I have two examples of code, the first works perfectly, but the second does not work and I get "error on page"

This works
   if (e.Item is GridEditableItem && e.Item.IsInEditMode) 
            { 
                GridEditableItem editItem = (GridEditableItem)e.Item; 
                TextBox textBox = (TextBox)editItem["DateOfBirth"].Controls[0]; 
                textBox.Text = Convert.ToDateTime(textBox.Text).ToString("dd-MMM-yy"); 
            }   

But unfortunately, this does not
   if (e.Item is GridEditableItem && e.Item.IsInEditMode) 
            { 
                GridEditableItem editItem = (GridEditableItem)e.Item; 
                TextBox textBox = (TextBox)editItem["DateOfBirth"].Controls[0]; 
                textBox.Text = Convert.ToDateTime(textBox.Text).ToString("dd-MM-yy"); 
            }   

With the second example, when I enter edit mode, the date is correctly displayed, eg 31/12/10, but if I want to change that date to 30th of December, I must enter it as follows, 12-30-10.

IOW, I can see the following 31-12-10 but must change that to 12-30-10 to successfully save the record.

But if I use the dd-MMM-yy format, I can change 31-Dec-10 to 30-Dec-10 and successfully save my record.

Have you any suggestions?

Thanks Shinu,

Mark

0
Mark Breen
Top achievements
Rank 1
answered on 28 Mar 2010, 01:01 PM
Hello Shinu,

I also want to set the expected format for inserts as well as Edit Items, I have tried doing this myself but cannot get it right, I suspect my lack of knowledge of the object model for the GridEditFormItem is why I do not know what to set.

I have tried the following but it just says "error on page" and I cannot see what to do, can you help?

  protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
        { 
            if ((e.Item is GridEditFormItem) && (e.Item.IsInEditMode)) 
            { 
                if (e.Item.OwnerTableView.IsItemInserted) 
                { 
                    GridEditableItem item = (GridEditableItem)e.Item; 
 
                    TextBox textBox = (TextBox)item["DateOfBirth"].Controls[0]; 
                    // If I uncomment the following line I cannot open the Add new record form 
 
                    //textBox.Text = Convert.ToDateTime(textBox.Text).ToString("dd-MMM-yy"); 
                } 
                else 
                { 
                    // This works well 
                    GridEditableItem item = (GridEditableItem)e.Item; 
                    TextBox textBox = (TextBox)item["DateOfBirth"].Controls[0]; 
                    textBox.Text = Convert.ToDateTime(textBox.Text).ToString("dd-MMM-yy"); 
                } 
            } 
        } 


Thanks Shinu,

Mark Breen
Ireland

0
Mark Breen
Top achievements
Rank 1
answered on 21 May 2010, 05:15 PM
Hello Telerik,

I still need some assistance on this issue, for this thread, 

as you can see I need to be able to enter dates in standard UK format dd/mm/yy using the autogenerated fields in the radGrid.

Can you please help as I am live with this system now and the customer is struggling with the dates.

thanks

Mark Breen

0
Mark Breen
Top achievements
Rank 1
answered on 24 May 2010, 10:01 AM
Hello All,

My problem was solved for me yesterday by Christoph Herold, - one of the speakers at Day of DNN Europe next week.

The simple solution, as bdukes also suggests, it to add another Language.  All you have to do is

1) Go to admin menu and select Languages
2) Add Language Pack
3) Select en-GB as the main and en-US as a fall back
4) Set the new pack as the default.

thats is, the problem is fixed.  the packs come pre-installed so i did not have to buy or install anything

thanks
Mark Breen
Ireland
BMW R80GS 1987

Tags
Grid
Asked by
Mark Breen
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Mark Breen
Top achievements
Rank 1
Share this question
or