Quantcast
Channel: Extended WPF Toolkit™ Community Edition
Viewing all articles
Browse latest Browse all 4964

New Post: DataGridControl - Formatting Auto-Generated DateTime? Column

$
0
0
Hi,
  1. "xcdg:Column" goes in the DataGridControl's Columns collection, ex :
<xcdg:DataGridControl x:Name="_dataGrid"
                            ItemsSource="{Binding Source={StaticResource cvsOrders}}" >   
            <xcdg:DataGridControl.Columns>
                <xcdg:Column FieldName="OrderID"
                             Title="Order"
                             Width="100"
                             IsMainColumn="True">
                </xcdg:Column>  
                <xcdg:Column FieldName="OrderDate"
                             Title="Order Date"
                             Width="120"
                             CellContentTemplate="{StaticResource DateDataTemplate}" />  
                <xcdg:Column FieldName="ShipRegion"
                              Visible="False" />               
            </xcdg:DataGridControl.Columns>
        </xcdg:DataGridControl>
  1. In the DataGridControl, you don't need to define all columns individually. If you don't, they will have a default width and look. You could set the AutoCreateColumns to False and only define the columns you want to see and apply specific configuration on them. We don't see the need in our design to have an equivalent to Microsoft AutoGeneratingColumn event. The binding can be done in XAML for each column.
  2. You can try with to use the CellContentStringFormat on a Column to format your date in xaml :
<xcdg:Column FieldName="OrderDate"
                             Title="Order Date"
                             Width="120"
                             CellContentStringFormat="{}{0:ddd MM/ dd / yy hh:mm tt}"/>
or in code-behind :
 _dataGrid.Columns[ "OrderDate" ].CellContentStringFormat = "{0:ddd MM/ dd / yy hh:mm tt}";
You can use XAML or code-behind for this, it's the same look.
  1. You can try with to use the CellContentTemplate on a Column to format your date in xaml as shown earlier.
    Or in code-behind with Resources defined in XAML:
_dataGrid.Columns[ "OrderDate" ].CellContentTemplate = this.Resources[ "DateDataTemplate" ] as DataTemplate;
or in full code-behind:
      var dataTemplate = new DataTemplate();
      var date = new FrameworkElementFactory( typeof( TextBlock ) );
      date.SetBinding( TextBlock.TextProperty, new Binding( "." ) { Converter = new DisplayedValueConverter() } );
      dataTemplate.VisualTree = date;

      _dataGrid.Columns[ "OrderDate" ].CellContentTemplate = dataTemplate;
XAML is simpler to use for DataTemplates and bindings.


For your situation, using the CellContentStringFormat is easier.

――――
Get more controls, features, updates and technical support with Xceed Toolkit Plus for WPF

Viewing all articles
Browse latest Browse all 4964

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>