Articles → LIGHT SWITCH 2011 → Convert UTC to local date and time in datagrid in lightswitch 2011

Convert UTC to local date and time in datagrid in

lightswitch 2011


























Software requirement











Prerequisite knowledge

















Steps of execution

















Create a new project





Click to Enlarge


Create new table





Click to Enlarge


Create a new screen





Click to Enlarge


Add data to the screen





Click to Enlarge


Add new column in the grid




using System;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
using System.Windows.Controls;
using System.Text;
using System.Windows.Controls.Primitives;
using System.Windows;
using System.Windows.Markup;
using System.Windows.Data;

namespace LightSwitchApplication {
  public partial class EditableUserProfilesGrid {
    partial void EditableUserProfilesGrid_Created() {
      // Write your code here.
      this.FindControl("grid").ControlAvailable += new EventHandler < ControlAvailableEventArgs > (EditableUserProfilesGrid_ControlAvailable);
    }

    void EditableUserProfilesGrid_ControlAvailable(object sender, ControlAvailableEventArgs e) {
      if (e.Control is DataGrid) {
        DataGrid _grid = (DataGrid) e.Control;
        _grid.Columns.Insert(3, GetLocalTimeColumns("DOJ", "DOJ"));
      }
    }
    private DataGridColumn GetLocalTimeColumns(string headerName, string bindingColumnName) {
      //  Declaration
      StringBuilder sbDataGridHeader;
      StringBuilder sbDataGridCell;
      DataGridTemplateColumn column;

      //  Initialization
      sbDataGridHeader = new StringBuilder(string.Empty);
      sbDataGridCell = new StringBuilder(string.Empty);
      column = new DataGridTemplateColumn();

      //  Create data template for grid column
      sbDataGridHeader.Append(@"<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>");
      sbDataGridHeader.Append(string.Format("<TextBlock Text='{0}'/>", headerName));
      sbDataGridHeader.Append(@"</DataTemplate>");

      //  Create data template for grid cell
      sbDataGridCell.Append(@"<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'");
      sbDataGridCell.Append(" xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'");
      sbDataGridCell.Append(" xmlns:local='clr-namespace:LightSwitchApplication;assembly=convert_to_local_datetime_demo.Client;'>");

      sbDataGridCell.Append("<Grid>");
      sbDataGridCell.Append("<Grid.Resources>");
      sbDataGridCell.Append("<local:DateTimeUtc2LocalValueConverter x:Key='DateTimeUtc2LocalValueConverter' />");
      sbDataGridCell.Append("</Grid.Resources>");

      sbDataGridCell.Append("<TextBlock Text='{Binding ");
      sbDataGridCell.Append(bindingColumnName);
      sbDataGridCell.Append(", Converter={StaticResource DateTimeUtc2LocalValueConverter}}'/>");
      sbDataGridCell.Append("</Grid>");
      sbDataGridCell.Append(@"</DataTemplate>");

      //  Assign data template to header
      Style headerStyle = new Style(typeof(DataGridColumnHeader));
      headerStyle.Setters.Add(new Setter {
        Property = DataGridColumnHeader.ContentTemplateProperty,
        Value = (DataTemplate) XamlReader.Load(sbDataGridHeader.ToString())
      });
      column.HeaderStyle = headerStyle;

      //  Assign data template to grid cell
      column.CellTemplate = XamlReader.Load(sbDataGridCell.ToString()) as DataTemplate;

      //  Return value
      return column;

    }
  }
  public class DateTimeUtc2LocalValueConverter: IValueConverter {
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
      return string.Format("{0} {1}", System.Convert.ToDateTime(value).ToLocalTime().ToShortDateString(), System.Convert.ToDateTime(value).ToLocalTime().ToLongTimeString());
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
      throw new NotImplementedException();
    }
  }
}



Code explanation


























<DataTemplate
    xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>
    <TextBlock  Text='DOJ'/>
</DataTemplate>






<DataTemplate
    xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
    xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
    xmlns:local='clr-namespace:LightSwitchApplication;assembly=convert_to_local_datetime_demo.Client;'>
    <Grid>
        <Grid.Resources>
            <local:DateTimeUtc2LocalValueConverter x:Key='DateTimeUtc2LocalValueConverter' />
        </Grid.Resources>
        <TextBlock Text='{Binding DOJ, Converter={StaticResource DateTimeUtc2LocalValueConverter}}'/>
    </Grid>
</DataTemplate>





Output





Click to Enlarge




Click to Enlarge




Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Friday, March 01, 2013

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250