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

Set label and datepicker format inside grid

2 Answers 84 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Andy Taslim
Top achievements
Rank 1
Andy Taslim asked on 04 Aug 2010, 11:06 AM
Hi guys,

I have an issue with setting the format of label and datepicker inside grid. I want to set "lblGridCreatedDate" and "lblGridCreatedDate" to have dd/MM/yyyy format in code behind. How can I do that?

Here is my code:
<telerik:GridTemplateColumn DataField="CreatedDate" HeaderText="Date" UniqueName="CreatedDate">
   <ItemTemplate>
       <asp:Label runat="server" ID="lblGridCreatedDate" Text='<%# Eval("CreatedDate") %>'></asp:Label>
   </ItemTemplate>
   <EditItemTemplate
       <telerik:RadDatePicker runat="server" ID="dtGridCreatedDate" Text='<%# Eval("CreatedDate") %>'></telerik:RadDatePicker>
    </EditItemTemplate>
</telerik:GridTemplateColumn>

Thank you

AGT

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 04 Aug 2010, 02:12 PM
Hello Andy,

Try the following code snippet in ItemDataBound event to change the DateFormat of the date shown in Label and RadDatePicker.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = (GridDataItem)e.Item;
            Label lb = (Label)item.FindControl("lblGridCreatedDate");
            lb.Text = Convert.ToDateTime(lb.Text).ToString("dd/MM/yyyy");
         }
       if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
        {
            GridEditFormItem editItem = (GridEditFormItem)e.Item;
            RadDatePicker datePick = (RadDatePicker)editItem.FindControl("dtGridCreatedDate");
            datePick.DateInput.DateFormat = "dd/MM/yyyy";
         }
    }

Regards,
Shinu.
0
Andy Taslim
Top achievements
Rank 1
answered on 05 Aug 2010, 06:20 AM
Thank you Shinu... it works like charm
Tags
Grid
Asked by
Andy Taslim
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Andy Taslim
Top achievements
Rank 1
Share this question
or