翻譯|使用教程|編輯:龔雪|2024-10-29 10:36:09.650|閱讀 111 次
概述:本文主要介紹如何使用DevExpress WinForms的Data Grid組件創建未綁定列,歡迎下載最新版組件體驗!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
本教程將介紹:
P.S:DevExpress WinForms擁有180+組件和UI庫,能為Windows Forms平臺創建具有影響力的業務解決方案。DevExpress WinForms能完美構建流暢、美觀且易于使用的應用程序,無論是Office風格的界面,還是分析處理大批量的業務數據,它都能輕松勝任!
獲取DevExpress WinForms v24.1正式版下載
DevExpress技術交流群10:532598169 歡迎一起進群討論
一個與Northwind數據庫的“Order Details”表綁定的數據網格應用程序。
1. 打開DevExpress WinForms Data Grid的智能標記,單擊Add Column來創建列。
2. 選擇此列并設置其屬性為唯一的字符串:“DiscountAmount”。
3. 將列的屬性設置為有效的數據類型。在本教程中,使用System.Decimal值。
1. 使用 屬性:單擊省略號按鈕打開Expression Editor(表達式編輯器)。
2. 創建一個簡單表達式,乘以三個字段:“Quantity”, “Unit Price”, “Discount”。
3. 將屬性設置為false來使該列只讀。
TIP:將列的設置為,將設置為c2,來將列值格式化為貨幣。
將列的屬性設置為true,來允許用戶在運行時修改未綁定列的表達式。
用戶可以在運行時從列的上下文菜單調用表達式編輯器來更改表達式。
1. 創建另一個列,設置為Total,為System.Decimal。
2. 選擇“gridView1”,并在屬性面板的“Events”頁面上訂閱事件。
注意:事件在每次即將顯示列值時和修改列單元格后(當需要發布數據時)觸發。
3. 如果e.IsGetData事件參數為true,則使用方法檢索Quantity、UnitPrice和Discount列的值。計算未綁定列的值,并將其分配給e.Value事件參數。
C#
void gridView_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e) { GridView view = sender as GridView; if(view == null) return; int rowIndex = e.ListSourceRowIndex; decimal unitPrice = Convert.ToDecimal(view.GetListSourceRowCellValue(rowIndex, "UnitPrice")); decimal quantity = Convert.ToDecimal(view.GetListSourceRowCellValue(rowIndex, "Quantity")); decimal discount = Convert.ToDecimal(view.GetListSourceRowCellValue(rowIndex, "Discount")); if (e.Column.FieldName != "Total") return; if (e.IsGetData) e.Value = unitPrice * quantity * (1 - discount); }
VB.NET
Private Sub gridView_CustomUnboundColumnData(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs) Dim view As GridView = TryCast(sender, GridView) If view Is Nothing Then Return End If Dim rowIndex As Integer = e.ListSourceRowIndex Dim unitPrice As Decimal = Convert.ToDecimal(view.GetListSourceRowCellValue(rowIndex, "UnitPrice")) Dim quantity As Decimal = Convert.ToDecimal(view.GetListSourceRowCellValue(rowIndex, "Quantity")) Dim discount As Decimal = Convert.ToDecimal(view.GetListSourceRowCellValue(rowIndex, "Discount")) If e.Column.FieldName <> "Total" Then Return End If If e.IsGetData Then e.Value = unitPrice * quantity * (1 - discount) End If End Sub
在編輯時,需要在未綁定列中保存更改。為此,您可以使用事件。
下面的代碼將Total列單元格中的更改保存到一個字典中,e.IsSetData事件參數指示未綁定列中的單元格值是否被修改。
C#
Dictionary<int, decimal> customTotals = new Dictionary<int, decimal>(); private void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e) { GridView view = sender as GridView; if(view == null) return; int rowIndex = e.ListSourceRowIndex; decimal unitPrice = Convert.ToDecimal(view.GetListSourceRowCellValue(rowIndex, "UnitPrice")); decimal quantity = Convert.ToDecimal(view.GetListSourceRowCellValue(rowIndex, "Quantity")); decimal discount = Convert.ToDecimal(view.GetListSourceRowCellValue(rowIndex, "Discount")); if (e.Column.FieldName != "Total") return; if (e.IsGetData) { if (!customTotals.ContainsKey(rowIndex)) customTotals.Add(rowIndex, unitPrice * quantity * (1 - discount)); e.Value = customTotals[rowIndex]; } if (e.IsSetData) { customTotals[rowIndex] = Convert.ToDecimal(e.Value); } }
VB.NET
Private customTotals As New Dictionary(Of Integer, Decimal)() Private Sub gridView1_CustomUnboundColumnData(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs) Dim view As GridView = TryCast(sender, GridView) If view Is Nothing Then Return End If Dim rowIndex As Integer = e.ListSourceRowIndex Dim unitPrice As Decimal = Convert.ToDecimal(view.GetListSourceRowCellValue(rowIndex, "UnitPrice")) Dim quantity As Decimal = Convert.ToDecimal(view.GetListSourceRowCellValue(rowIndex, "Quantity")) Dim discount As Decimal = Convert.ToDecimal(view.GetListSourceRowCellValue(rowIndex, "Discount")) If e.Column.FieldName <> "Total" Then Return End If If e.IsGetData Then If Not customTotals.ContainsKey(rowIndex) Then customTotals.Add(rowIndex, unitPrice * quantity * (1 - discount)) End If e.Value = customTotals(rowIndex) End If If e.IsSetData Then customTotals(rowIndex) = Convert.ToDecimal(e.Value) End If End Sub
更多產品資訊及授權,歡迎“”!
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都網