日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

DataGrid数据绑定

發布時間:2025/3/15 编程问答 13 豆豆
生活随笔 收集整理的這篇文章主要介紹了 DataGrid数据绑定 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

后臺數據綁定

用戶場景是生成報表,展示公司各員工每個月的績效

數據結構

包括報表和單個員工績效兩個實體

public class Report {/// <summary>/// 統計時間/// </summary>public string StatisticalDate { get; set; }public List<ReportDetail> ReportDetails { get; set; } } public class ReportDetail {/// <summary>/// 職員姓名/// </summary>public string EmployeeName { get; set; }/// <summary>/// 統計數據/// </summary>public decimal Data { get; set; } }

關鍵代碼

DataGrid dataGrid = new DataGrid(); var _ds = new DataSet("Test"); Dt = _ds.Tables.Add("月度績效表"); //create columns //創建列 Dt.Columns.Add("月份"); foreach (var item in reports[0].ReportDetails) {Dt.Columns.Add(item.EmployeeName); } //fill data to rows //賦值數據 for(int i=0;i< reports.Count;i++) {var theRow = Dt.NewRow();theRow[0] = reports[i].StatisticalDate;for (int j = 0; j < reports[i].ReportDetails.Count; j++){theRow[j+1] = reports[i].ReportDetails[j].Data;}Dt.Rows.Add(theRow); } //數據綁定 dataGrid.ItemsSource = Dt.AsDataView(); //將控件添加到Grid MyGrid.Children.Add(dataGrid);

示例代碼

https://github.com/zLulus/NotePractice/blob/dev3/WPF/WpfDemo/Bind/DataGridBackgroundBind.xaml
https://github.com/zLulus/NotePractice/blob/dev3/WPF/WpfDemo/Bind/DataGridBackgroundBind.xaml.cs

其他:列頭重復解決方案

當前用戶場景,如果遇到行列互換,即將員工姓名和月份互換,可能出現列名相同的問題(員工同名),則最好將列頭綁定改為員工姓名+員工編號,保證唯一性,前端只顯示名稱,綁定"名稱+ID"

前端數據綁定

數據結構

包括教師和教師信息擴展兩個實體

public class Teacher {public string SchoolNumber { get; set; }public string Name { get; set; }public string Sex { get; set; }public TeacherDetailInfo TeacherDetailInfo { get; set; } } public class TeacherDetailInfo {public DateTime EntryTime { get; set; }public string Address { get; set; } }

關鍵代碼

<DataGrid ItemsSource="{Binding }" AutoGenerateColumns="False" CanUserAddRows="False"><DataGrid.Columns><DataGridTextColumn Header="編號" Binding="{Binding SchoolNumber}"/><DataGridTextColumn Header="姓名" Binding="{Binding Name}"/><DataGridTextColumn Header="性別" Binding="{Binding Sex}"/><!--格式化日期--><DataGridTextColumn Header="入職時間" Binding="{Binding Path=TeacherDetailInfo.EntryTime, StringFormat=\{0:yyyy年MM月dd日\}}"/><!--如果這里是雙向綁定,則是下面的寫法,Mode是雙向(TwoWay),觸發器是變化即觸發--><!--<DataGridTextColumn Header="入職時間" Binding="{Binding Path=TeacherDetailInfo.EntryTime,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>--><DataGridTextColumn Header="住址" Binding="{Binding Path=TeacherDetailInfo.Address}"/></DataGrid.Columns> </DataGrid>

示例代碼

https://github.com/zLulus/NotePractice/blob/dev3/WPF/WpfDemo/Bind/DataGridBindMultiData.xaml
https://github.com/zLulus/NotePractice/blob/dev3/WPF/WpfDemo/Bind/DataGridBindMultiData.xaml.cs

轉載于:https://www.cnblogs.com/Lulus/p/9726375.html

總結

以上是生活随笔為你收集整理的DataGrid数据绑定的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。