[英]How to do math and combine data in datagrid WPF
我已將 excel 中的數據導入 wpf 應用程序中的數據網格,我正在嘗試對數據進行一些數學運算並合並一些列。請告知處理此問題的最佳方法。
為了解決這個問題,我試圖創建一個 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; }
}
如何將 map 或將我的 C# class 對象鏈接到數據網格中的列?
這是簡單的方法還是我需要將所有內容放入 SQL 數據庫並編寫查詢,我對 SQL 很滿意。 任何建議將不勝感激。 我試圖從很長一段時間內弄清楚這一點。
您始終可以使用額外的屬性擴展您的 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(); } }
}
我將從學習有關DataGrid
的一些基礎知識開始。 此頁面應該對您正在做的事情有所幫助: https://www.wpf-tutorial.com/datagrid-control/custom-columns/
您可以在<DataGrid.Columns>
標記之間定義要在DataGrid
中顯示的任何列。 每列都有一個Binding
屬性,用於設置 class ( Person
) 的哪個屬性通過數據綁定鏈接到該列。
至於做數學,您可以為Person
創建只讀屬性。 在只讀屬性中,您可以根據其他屬性進行計算,然后返回結果。 要顯示結果,您可以將 readonly 屬性綁定到DataGrid
上的列。 請記住,如果您將Person
class 中的數據在運行時可編輯並更新數學結果,則需要讓Person
實現INotifyPropertyChanged
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.