We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.

Format a date value in GridUnboundColumns format property

Thread ID:

Created:

Updated:

Platform:

Replies:

125445 Aug 18,2016 12:23 PM Sep 1,2016 09:58 PM WPF 12
loading
Tags: SfDataGrid
Harald Betzler
Asked On August 18, 2016 12:23 PM

Hi,

I have to use a GridUnboundColumn with format:  "'Datum: {validDate}'"

The result is: "Datum: 28.10.2016 00:00:00"

The expected result is: "Datum: 28.10.2016"

How can I achieve that?

Regards

Harald

Jai Ganesh S [Syncfusion]
Replied On August 19, 2016 07:51 AM

Hi Harald, 
 
You can achieve your requirement to format the DateTime value in GridUnboundColumn by wiring the QueryUnboundColumnValue event like below, 
 
this.datagrid.QueryUnboundColumnValue += datagrid_QueryUnboundColumnValue; 
 
 
void datagrid_QueryUnboundColumnValue(object sender, GridUnboundColumnEventsArgs e) 
{ 
    var value = e.Value.ToString(); 
    DateTime dt = Convert.ToDateTime(value); 
    e.Value = "Datum:" + dt.ToString("M.d.yyyy"); 
} 
 
 
Screen Shot: 
 
 
Regards, 
Jai Ganesh S 


Harald Betzler
Replied On August 21, 2016 10:32 AM

Hi Jai,

thank you for the solution. 

Is there a way to achieve the target by xaml?

Imagine you have 8 unbound columns with date values to format into german date format. But each column in another format (e.g. "abc: 01.01.2016", "def: 02.02.2016", "ghi: 03.03.2016", etc).

Then you have to identity the column in the eventhandler (by column index or by column header title). But this is very fragile to changes (adding / removing columns). Therefore it should better realized by xaml.

Can you help me?

Regards
Harald

Harald Betzler
Replied On August 21, 2016 10:43 AM

I have a further need to format the date within the Format property of GridUnBoundColumn:
Format="'{ValidFrom} - {ValidTo} (von {DelegatorSalutation} {DelegatorFirstname} {DelegatorLastname})'"
In this case the grid displays:  01.08.2016 00:00:00 - 15.08.2016 00:00:00 (von Herrn Max Müller)

But it should be: 01.08.2016 - 15.08.2016 (von Herrn Max Müller)

I think, this is not resolvable with a eventhandler in code behind.

Regards
Harald



Jai Ganesh S [Syncfusion]
Replied On August 22, 2016 08:21 AM

Hi Harald, 
 
Please find the responses for your queries as below, 
 
Query 1: 
UnboundColumn provides support for specify expressions only. You can find the details on below documentation link, 
 
 
Query 2: 
 
Your requirement can be achieved using QueryUnboundColumnValue event.  
 
void datagrid_QueryUnboundColumnValue(object sender, GridUnboundColumnEventsArgs e) 
{ 
    var data = e.Value as BusinessObjects; 
    string validFrom=null, validTo=null; 
    if (data != null) 
    { 
            validFrom = data.ValidFrom.ToString("M.d.yyyy"); 
            validTo = data.ValidTo.ToString("M.d.yyyy"); 
    } 
 
    e.Value = validFrom + "-" + validTo; 
 
} 
 
In the above code, we have format the two DateTime values and append it by “–“ . Likewise, you can get the values and format it in QueryUnboundColumnValue event. 
 
 
Regards, 
Jai Ganesh S 


Harald Betzler
Replied On August 23, 2016 08:39 AM

Hi Jai,

thank you for the example. But due to MVVM concerns it should be avoided to reference the model from the view. "BusinessObjects" is typically defined in the model (but not in your simplified example). 

Back to my requirement to build this string in an unbound column:
Format="'{ValidFrom} - {ValidTo} (von {DelegatorSalutation} {DelegatorFirstname} {DelegatorLastname})'"
The grid displays:  01.08.2016 00:00:00 - 15.08.2016 00:00:00 (von Herrn Max Müller)

But it should display: 01.08.2016 - 15.08.2016 (von Herrn Max Müller)

It would be very helpful to format date values (and perhaps other number values) directly in the xaml column definition like this suggestion (or with other syntax):
Format="'{ValidFrom(M.d.yyyy)} - {ValidTo(M.d.yyyy)} (von {DelegatorSalutation} {DelegatorFirstname} {DelegatorLastname})'"

Is there really no chance to avoid code behind?

Sure, I could form this string in the viewmodel. But viewmodel should not handle formatting problems. This should handled in the view.

At the moment I think, that Syncfusions SfDataGrid is not really compatible with MVVM concept.

Regards
Harald



Jai Ganesh S [Syncfusion]
Replied On August 24, 2016 10:01 AM

Hi Harald, 
 
As we said in our previous update, you cannot set the DateTime format through Xaml in UnboundColumn. Now we have modified our sample as a MVVM pattern by wiring the QueryUnboundColumnValue event through behavior, 
 
<interactivity:Interaction.Behaviors> 
       <local:InitialSetupBehaviour/> 
 </interactivity:Interaction.Behaviors> 
 
public class InitialSetupBehaviour : Behavior<SfDataGrid> 
    { 
        protected override void OnAttached() 
        { 
            this.AssociatedObject.QueryUnboundColumnValue += AssociatedObject_QueryUnboundColumnValue; 
        } 
 
        void AssociatedObject_QueryUnboundColumnValue(object sender, GridUnboundColumnEventsArgs e) 
        { 
            var data = e.Value as BusinessObjects; 
            string validFrom = null, validTo = null; 
            if (data != null) 
            { 
                validFrom = data.ValidFrom.ToString("M.d.yyyy"); 
                validTo = data.ValidTo.ToString("M.d.yyyy"); 
            } 
 
            e.Value = validFrom + "-" + validTo; 
        } 
         
        protected override void OnDetaching() 
        { 
            this.AssociatedObject.QueryUnboundColumnValue -= AssociatedObject_QueryUnboundColumnValue; 
        } 
    } 
 
 
Regards, 
Jai Ganesh S 


Harald Betzler
Replied On August 30, 2016 01:01 PM

Hi Jai,
thank you for the behavior solution.

Now I've a problem with the observability. The expected string is formatted by the behavior class. But only while loading the data.

I attached your sample (extended by some changes).
I changed column 2 and 3 from Quantity to ValidFrom and Unit Price to ValidTo. These are the single values of the concatenated string in column 4.
I added a button and a click event handler, that changes the ValidFrom and ValidTo values in the observable collection GDSource in the view model.

As expected the values in column 2 and 3 are changed. But as not expected the concatenated string in column 4 ist noch changed.

How can this be realized?

regards
Harald

Attachment: SfGridDemo_c587fea4.zip

Jai Ganesh S [Syncfusion]
Replied On August 31, 2016 10:02 AM

Hi Harald, 
 
You can achieve your requirement to change the UnboundColumn value at runtime by calling the UpdateUnboundColumns method like below, 
 
public void ChangeDate(object sender, RoutedEventArgs routedEventArgs) 
   { 
            var vm = (ViewModel) DataContext; 
 
            var item = vm.GDCSource.FirstOrDefault(); 
            if (item != null) 
            { 
                item.ValidFrom = DateTime.Today; 
                item.ValidTo = DateTime.Today; 
            } 
             
            datagrid.UpdateUnboundColumns(item); 
             
     } 
 
You can also refresh the UnboundColumn value in CurentCellEndEdit event. Please refer the following UG for more details, 
 
 
Regards, 
Jai Ganesh S 


Harald Betzler
Replied On August 31, 2016 10:48 AM

Hi Jai,

thank you for the UpdateUnboundColumns hint.

I realize, that the sample was too simple. I implemented the button and the click eventhandler to demonstrate, that the unbound column won't be recalculated.

But in reality my data source is updated by another view - as usual in MVVM. And another view cannot explicitly trigger an update of the unbound columns. Views typically do not know about other views.

The grid will be notified about data changes from the data source by INotifyPropertyChanged. This works for bound column and should also work for unbound columns. Unbound columns have to be updated automatically - otherwise they do not work according the binding concept of MVVM.

Regards
Harald

Jai Ganesh S [Syncfusion]
Replied On September 1, 2016 08:47 AM

Hi Harald, 
 
You can achieve your requirement in MVVM pattern by update the UnboundColumns through RecordPropertyChanged event in Behavior class. 
 
this.AssociatedObject.ItemsSourceChanged += AssociatedObject_ItemsSourceChanged; 
 
void AssociatedObject_ItemsSourceChanged(object sender, GridItemsSourceChangedEventArgs e) 
{ 
    this.AssociatedObject.View.RecordPropertyChanged += View_RecordPropertyChanged; 
} 
 
void View_RecordPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) 
{ 
    var record=(sender as BusinessObjects); 
    this.AssociatedObject.UpdateUnboundColumns(record); 
} 
 
 
If the above solution is not working in your end, then please share how you update the data source using another view. This would be more helpful for us to provide the prompt solution. 
 
Regards, 
Jai Ganesh S 


Harald Betzler
Replied On September 1, 2016 02:28 PM

Hi Jai,

thank you. That's what I need.

Regards
Harald

Jai Ganesh S [Syncfusion]
Replied On September 1, 2016 09:58 PM

Hi Harald, 
 
Thank you for the update. 
 
Please let us know if you need further assistance on this. 
 
Regards, 
Jai Ganesh S 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;