翻譯|使用教程|編輯:龔雪|2024-11-12 10:38:12.673|閱讀 130 次
概述:本文主要介紹如何使用DevExpress WinForms的Data Grid組件綁定到實體框架數(shù)據(jù)源,歡迎下載最新版組件體驗!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
在本教程中,您將學(xué)習(xí)如何將DevExpress WinForms的網(wǎng)格控件綁定到實體框架數(shù)據(jù)源、如何使用數(shù)據(jù)注釋屬性來更改網(wǎng)格顯示和管理數(shù)據(jù)的方式,以及如何將單元格值更改發(fā)送回數(shù)據(jù)源。
P.S:DevExpress WinForms擁有180+組件和UI庫,能為Windows Forms平臺創(chuàng)建具有影響力的業(yè)務(wù)解決方案。DevExpress WinForms能完美構(gòu)建流暢、美觀且易于使用的應(yīng)用程序,無論是Office風(fēng)格的界面,還是分析處理大批量的業(yè)務(wù)數(shù)據(jù),它都能輕松勝任!
獲取DevExpress WinForms v24.1正式版下載
DevExpress技術(shù)交流群11:749942875 歡迎一起進(jìn)群討論
從一個項目開始,它有一個Windows Form和一個空的GridControl,該解決方案還包括一個默認(rèn)名為Model1的實體框架模型,您可以雙擊它來查看圖表上的可視化表示。在本教程中,使用修改版本的AdventureWorks數(shù)據(jù)庫,它只包含一個表,其中只有幾個字段。
將實體框架模型綁定到網(wǎng)格控件的最簡單方法是使用數(shù)據(jù)源配置向?qū)В{(diào)用DevExpress WinForms網(wǎng)格控件的智能標(biāo)記并單擊數(shù)據(jù)源向?qū)А?
選擇Entity Framework技術(shù)并選擇現(xiàn)有的數(shù)據(jù)連接或創(chuàng)建一個新的數(shù)據(jù)連接,在本教程中使用到樣例Microsoft AdventureWorksDW2008R2數(shù)據(jù)庫的現(xiàn)有連接,單擊Next繼續(xù)。
在下一頁,您將被要求選擇所需的綁定模式。選擇Binding using Binding Source組件選項,然后單擊Next。
最后,選擇要在網(wǎng)格控件中顯示的表格。
網(wǎng)格控件現(xiàn)在綁定到EF數(shù)據(jù),并且已經(jīng)生成了所有必需的代碼,您可以在.cs文件中看到自動生成的代碼。現(xiàn)在運(yùn)行應(yīng)用程序來查看結(jié)果。
下面是一些關(guān)于grid如何顯示其綁定數(shù)據(jù)的觀察。
所有這些默認(rèn)操作都可以使用DevExpress WinForms GridControl設(shè)置進(jìn)行更改,另一種方法是更改數(shù)據(jù)字段屬性或數(shù)據(jù)注釋屬性。當(dāng)您有多個控件綁定到同一數(shù)據(jù)源時,這可能會派上用場。這樣您就不必設(shè)置相同的規(guī)則,看看這是怎么回事。
打開包含表格數(shù)據(jù)模型的DimProducts.cs文件,在這里您可以看到所有可用的數(shù)據(jù)字段,它們被聲明為DimProduct類的屬性。
C#
public partial class DimProduct { public int ProductKey { get; set; } public string EnglishProductName { get; set; } public Nullable<decimal> DealerPrice { get; set; } public string EnglishDescription { get; set; } public Nullable<System.DateTime> StartDate { get; set; } public Nullable<System.DateTime> EndDate { get; set; } }
VB.NET
Partial Public Class DimProduct Public Property ProductKey() As Integer Public Property EnglishProductName() As String Public Property DealerPrice() As Decimal? Public Property EnglishDescription() As String Public Property StartDate() As Date? Public Property EndDate() As Date? End Class
向這些屬性添加數(shù)據(jù)屬性,看看這會如何改變網(wǎng)格操作。
注意:應(yīng)用程序應(yīng)該引用System.ComponentModel.DataAnnotations庫來完成此操作。
C#
[Required] public string EnglishProductName { get; set; }
VB.NET
<Required> Public Property EnglishProductName() As String
運(yùn)行應(yīng)用程序來確保不再允許空值。
對于DealerPrice字段,添加DataType屬性,以指示應(yīng)該將這些整數(shù)值視為貨幣數(shù)據(jù)。此外,應(yīng)用Range屬性來設(shè)置允許的最小值和最大值。
C#
[DataType(DataType.Currency), Range(200, 5000)] public Nullable<decimal> DealerPrice { get; set; }
VB.NET
<DataType(DataType.Currency), Range(200, 5000)> Public Property DealerPrice() As Decimal?
C#
[DataType(DataType.MultilineText), Display(Name = "Description", Description = "Detailed product description")] public string EnglishDescription { get; set; }
VB.NET
<DataType(DataType.MultilineText), Display(Name := "Description", Description := "Detailed product description")> Public Property EnglishDescription() As String
運(yùn)行應(yīng)用程序來查看用于列標(biāo)頭及其工具提示的更新標(biāo)題,另外注意單元格現(xiàn)在使用MemoEdit,因為您將數(shù)據(jù)類型指定為多行文本。
C#
[DataType(DataType.Text)] public Nullable<System.DateTime> StartDate { get; set; } [DisplayFormat(DataFormatString = "MMMM/yyyy")] public Nullable<System.DateTime> EndDate { get; set; }
VB.NET
<DataType(DataType.Text)> Public Property StartDate() As Date? <DisplayFormat(DataFormatString := "MMMM/yyyy")> Public Property EndDate() As Date?
啟動應(yīng)用程序,看到StartDate列中沒有顯示任何特殊的編輯器。EndDate列保留了編輯器,但格式發(fā)生了變化。
要確保網(wǎng)格控件中的單元格值更改被發(fā)送回數(shù)據(jù)源,您需要添加幾行代碼。當(dāng)網(wǎng)格引發(fā)事件時保存更改,您需要訪問數(shù)據(jù)源上下文,因此必須更改變量的作用域,使其對Form類中的所有方法都可用。在處理程序中,需要做的就是調(diào)用SaveChanges方法。
C#
DXApplication.AdventureWorksDW2008R2Entities dbContext; private void gridView1_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e) { dbContext.SaveChanges(); }
VB.NET
Private dbContext As DXApplication.AdventureWorksDW2008R2Entities Private Sub gridView1_RowUpdated(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.RowObjectEventArgs) dbContext.SaveChanges() End Sub
現(xiàn)在運(yùn)行應(yīng)用程序并更改Dealer Price列中的值,將焦點移動到另一行,來確保引發(fā)事件。然后關(guān)閉應(yīng)用程序并再次運(yùn)行它,您將看到該值已保存。
更多產(chǎn)品資訊及授權(quán),歡迎“”!
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都網(wǎng)