简体   繁体   English

如何在数据网格 WPF 中进行数学运算和组合数据

[英]How to do math and combine data in datagrid WPF

I have imported data from excel to data grid in wpf application and I am trying to do some math on the data and also combine some columns.Please advise as what would be the best way to handle this.我已将 excel 中的数据导入 wpf 应用程序中的数据网格,我正在尝试对数据进行一些数学运算并合并一些列。请告知处理此问题的最佳方法。

to solve this I was trying to create a C# class, but I do not know how to map or link my class objects to a column in data grid?为了解决这个问题,我试图创建一个 C# class,但我不知道如何 map 或将我的 ZA2F2ED4F2ED4F2EBC2CBBD4C 对象链接到 A 列 inA298EBC2CBBD4C0 数据?

  class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string City { get; set; }
        public int Salary { get; set; }
    }

how can I map or link my C# class objects to a column in Data grid?如何将 map 或将我的 C# class 对象链接到数据网格中的列?

is it the easy way or I need to put everything into SQL database and write queries, I am good with SQL.这是简单的方法还是我需要将所有内容放入 SQL 数据库并编写查询,我对 SQL 很满意。 Any advise will be highly appreciated.任何建议将不胜感激。 I am trying to figure this out from a real long time.我试图从很长一段时间内弄清楚这一点。

You can always extend your Person class with extra properties that expose getter only with whatever combinations you want... Something like...您始终可以使用额外的属性扩展您的 Person class ,这些属性仅通过您想要的任何组合公开吸气剂......就像......

class Person
    {
        public int Id { get; set; }
        // added the = ""; just to prevent nulls 
        public string Name { get; set; } = "";
        public string City { get; set; } = "";
        public int Salary { get; set; }
        // Now you can show these columns directly in the grid -- just an example
        public decimal MonthlySalary { get { return Math.Round(Salary / 12.0, 2 ); } }
        public string NameAndCity { get { return Name.Trim() + ",  " + City.Trim(); } }
    }

I'd start by learning some basics regarding the DataGrid .我将从学习有关DataGrid的一些基础知识开始。 This page should be helpful for what you're doing: https://www.wpf-tutorial.com/datagrid-control/custom-columns/此页面应该对您正在做的事情有所帮助: https://www.wpf-tutorial.com/datagrid-control/custom-columns/

You would define whatever columns you want to show in the DataGrid between the <DataGrid.Columns> tags.您可以在<DataGrid.Columns>标记之间定义要在DataGrid中显示的任何列。 Each column has a Binding property which you use to set which property of your class ( Person ) is linked to that column via data bining.每列都有一个Binding属性,用于设置 class ( Person ) 的哪个属性通过数据绑定链接到该列。

As for doing math, you can create readonly properties for Person .至于做数学,您可以为Person创建只读属性。 Inside the readonly properties you could do calculations based on other properties and then return the result.在只读属性中,您可以根据其他属性进行计算,然后返回结果。 To display the result, you would bind the readonly property to a column on the DataGrid .要显示结果,您可以将 readonly 属性绑定到DataGrid上的列。 Keep in mind that if you the data in your Person class to be editable at runtime and have the math results update, you'll need to have Person implement INotifyPropertyChanged .请记住,如果您将Person class 中的数据在运行时可编辑并更新数学结果,则需要让Person实现INotifyPropertyChanged

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM