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. Image for the cookie policy date

display column header and data based on user selection of units system used: U.S imperial unit V.S Metric units

I plan to use/bind sfDataGrid to display a XML deSerialized data (System.Xml.Serialization namespace, based on xsd schema), the xml data is in U.S. units (feet, lbf) and is the result of my calculation algorithm, the corresponding Metric units is Meter, Newton.  When I was using WPF toolkit DataGrid, I bind the column using multibinding with Converter, the two inputs to the converter is the original value Path in xml and UnitSystem selected NotificationObject (0: U.S 1 Metric).  The problem is that if I cut and paste data to clip board, it shows its original U.S unit data, the converter only changes the data presentation in WPF, and cut paste to clipboard totally ignores the converter action.

I could have manually added getter and setter code into XSD generated System.Xml.Serialization  class, because my XSD is very deep, it will be tremendous amount of coding, and logically not correct, why true data  needs to be manipulated with its presentation unit. 

What is the strategy to display data dynamically (don't change the underneath data original format), and still keep cut paste as what you see is what you get.  Do I need to use converter?

My question is not related to my using xml object, it can be DataTable object,  there is always a need to present who DataTable data in different Units, and we should not change DataTable data itself.

Thanks for tips.


11 Replies

MK Muthukumar Kalyanasundaram Syncfusion Team February 27, 2017 06:13 PM UTC

Hi Rulong Chen, 

Thank you for contacting Syncfusion support. 

We have analyzed your query. In SfDataGrid, if you want to copy and paste the selected record, the copied record value will added only in the underlying collection. If you want to paste the copied data in desired format by handling the CopyGridCellContent event of the SfDataGrid as like the below code snippet, 

Code Snippet: 
private void DataGrid_CopyGridCellContent(object sender, GridCopyPasteCellEventArgs e) 
{ 
    if (e.Column.MappingName == "EmployeeID"){ 
    var formattedValue = dataGrid.View.GetPropertyAccessProvider().GetFormattedValue((e.OriginalSender as SfDataGrid).SelectedItem, "EmployeeID"); 
        e.ClipBoardValue = formattedValue; 
 
  

If your requirement is different from this, could you please provide simple sample along with the replication procedure? This would be helpful to provide an appropriate solution.    

Please let us know if you have any query. 

Regards, 
Muthukumar K 
  



RC rulong chen March 8, 2017 10:31 PM UTC

  <syncfusion:GridNumericColumn x:Name="WCWindPressrDG1Col" MappingName="WindPressure" AllowEditing="True" HeaderText="{Binding Path=WindPreasureTitle}"/>

This one doesn't work in HeaderText binding,
MappingName works

public string _WindPreasureTitle;
        public string WindPreasureTitle
        {
            get
            {
                return _WindPreasureTitle;
            }
            set
            {
                //no setter, but a way to post a event. if calling from outside
                string tempstr = Resources.Resource.WindPreasureTitle;
                if (_UnitsSystemUsed == 0)
                    _WindPreasureTitle = tempstr + " Psf:";
                else if (_UnitsSystemUsed == 1)
                    _WindPreasureTitle = tempstr + " Pa:";
                RaisePropertyChanged("WindPreasureTitle");
            }
        }

if the unitssystemused is US unit, the header should be: Wind Preasure Psf otherwise it should be Pa.

public double _WindPressure = 0.0; //Psf
        public double WindPressure
        {
            get
            {
                if (_UnitsSystemUsed == 0)
                    return _WindPressure;
                else
                    return _WindPressure * Model.ConversionRatio.PsfToPa;
            }
            set
            {
                if (value < 0.0)
                {
                    List<string> errors = new List<string>();
                    errors.Add("Wind Pressure can not be negative");
                    SetErrors("WindPressure", errors);
                    _WindPressure = value;
                }
                else
                {
                    ClearErrors("WindPressure");
                  
                    if (_UnitsSystemUsed == 0)
                    {
                        _WindPressure = value;
                        _WindSpeed = Math.Sqrt(_WindPressure / _AirDensity);
                    }
                    else if (_UnitsSystemUsed == 1 )
                    {
                        _WindPressure = value / Model.ConversionRatio.PsfToPa;
                        _WindSpeed = Math.Sqrt(_WindPressure / _AirDensity);
                    }
                
                    RaisePropertyChanged("WindPressure");
                    RaisePropertyChanged("WindSpeed");
                }
            }
        }




MK Muthukumar Kalyanasundaram Syncfusion Team March 9, 2017 12:24 PM UTC

Hi Rulong Chen,  
 
Thanks for the update. 
 
We have analyzed your query. We have unable to reproduce your reported “HeaderText are not binding in Sfdatagrid”. For your reference, we have attached the sample in below location, in that sample we have set the header text in view model class. Could you please refer to it. 
 

If you are still facing the issue, could you please revert by modifying the attached sample based on your application along with the replication procedure? This would be helpful to provide an appropriate solution.    

Please let us know if you have any query. 

Regards, 
Muthukumar K 



RC rulong chen March 10, 2017 06:49 AM UTC

Hi Muthukumar,

Could you provide me more info about "{Binding Path=CustomHeaderText, Source={StaticResource viewModel}}" so my binding must say Path=,then what is source for? I think my mistake is that I didn't specify the source properly, and I need to know how to specify it.

I normally don't do this way to specify the datacontext,

       
 

I have many usercontrols, I just specify the dataconext as in MainWindow.xmal.c# load section as
 this.SagTab.DataContext = sagTensionViewModel;
where SagTab is a x:name

so How do I specify the Source, because it may not be static.

I have many XAML view classes, and two big viewmodel class, if I do it as you did through
 
       
   
   
       
   

will WPF created individual instance of ViewModel for each XAML classes? That is the reason I did in MainWindow constractor, I created ViewModel in code, then assign to each XAML object
in code,

           ViewModel EconAnalysisVwMdl = new ViewModel();
            this.EconTab.DataContext = EconAnalysisVwMdl;
            SagViewModel sagTensionViewModel = new SagViewModel(EconAnalysisVwMdl.UnitAndVoltageTypeSelect,
               EconAnalysisVwMdl.AllTitleNames,
                EconAnalysisVwMdl.Conductor1Properties,
                EconAnalysisVwMdl.Conductor2Properties,
                EconAnalysisVwMdl.Conductor3Properties,
                EconAnalysisVwMdl.Conductor4Properties);
            this.SagTab.DataContext = sagTensionViewModel;


I guess real life is just a little bit more complicated.
 Bottom question is how to configure  Souce , what is it pointing to, if I created ViewModel in code and assign to datacontext in code.





RC rulong chen March 10, 2017 07:44 AM UTC

I just changed MainWIndow.XAML and MainWindow.xaml.cs to reflect my pattern of assigning datacontext, the headertext doesn't bind again, this is the exactly problem I am having.
Please see attached zip, so the framework is not endorsing my way of coding.



Attachment: MainWindow_41ca87b8.zip


MK Muthukumar Kalyanasundaram Syncfusion Team March 13, 2017 01:15 PM UTC

Hi Rulong Chen,  
 
Thanks for the update. 
 
We have checked your query. You can achieve your requirement by setting SetBinding method. For your reference, we have attached the sample and code snippet in below location, 
 
Code Snippet:  
 
ViewModel myVwMdl = new ViewModel(); 
this.DataContext = myVwMdl; 
 
 
Binding binding = new Binding 
{ 
    Source = myVwMdl, 
    Path = new PropertyPath("CustomHeaderText"), 
};             
BindingOperations.SetBinding(dataGrid.Columns[3], GridColumn.HeaderTextProperty, binding); 
 
 
                                                                                                                                                                                                                                             
For more details about binding  Source. 
 
 
 
Please let us know if you have any concerns. 
 
Regards, 
Muthukumar K 



RC rulong chen March 16, 2017 08:10 PM UTC

Thanks Muthukumar for answer me so many times,

now I have another question,  now I can display the data ok, but my data is dynamic, when user click a re calculation result, my ObservableCollection<OneSagWeatherCalcResult> changed, both the count in the collection and item in the collection changes, but I did not see the Datagrid get up dated.



RC rulong chen March 16, 2017 08:14 PM UTC

this is good suggestion and works, but in term of amount of coding, it is not less, so instead I just did this.C1IITlstn.HeaderText = myVwMdl.AllTitleNames.TotalTensionTitle; after catch the event.



RC rulong chen March 16, 2017 08:42 PM UTC

never mind I figured out I mixed List<T> and ObservableCollection<T> in one chart different series, it stopped working.


RC rulong chen March 17, 2017 11:05 PM UTC

Sorry for the last question,
In you sample code, you have examples of export one datagrid to one excel workbook and worksheet, I have a usercontrol that has 5 datagrid, I want to export all five datagrid to one excel workbook in five separate excel sheet, how can I do that.




MK Muthukumar Kalyanasundaram Syncfusion Team March 20, 2017 06:11 AM UTC

Hi Rulong chen, 
We have analyzed your query. If you want to export multiple dataGrid to single workbook in a separate worksheet, you have pass the particular worksheet in ExportToExcel method like the below code, 
                                                                                                                                                                                                        
Code Snippet:  
private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    var options = new ExcelExportingOptions(); 
    options.ExcelVersion = ExcelVersion.Excel2013;             
    var excelEngine1 = new ExcelEngine(); 
  var workBook1 = excelEngine1.Excel.Workbooks.Create(); 
  dataGrid1.ExportToExcel(dataGrid1.View, options, workBook1.Worksheets[0]); 
    dataGrid2.ExportToExcel(dataGrid2.View, options, workBook1.Worksheets[1]); 
    dataGrid3.ExportToExcel(dataGrid3.View, options, workBook1.Worksheets[2]); 
    // Saving the workbook. 
    workBook1.SaveAs("sample.xlsx"); 
    //Message box confirmation to view the created spreadsheet. 
    if (MessageBox.Show("Do you want to view the workbook?", "Workbook has been created", 
                                MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.Yes) 
    { 
        //Launching the Excel file using the default Application.[MS Excel Or Free ExcelViewer] 
        System.Diagnostics.Process.Start("sample.xlsx"); 
    } 
} 


Please let us know if you have any query. 

Regards, 
Muthukumar K 


Loader.
Up arrow icon