翻譯|使用教程|編輯:龔雪|2024-10-29 10:36:09.650|閱讀 123 次
概述:本文主要介紹如何使用DevExpress WinForms的Data Grid組件創(chuàng)建未綁定列,歡迎下載最新版組件體驗!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
本教程將介紹:
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ù)交流群10:532598169 歡迎一起進(jìn)群討論
一個與Northwind數(shù)據(jù)庫的“Order Details”表綁定的數(shù)據(jù)網(wǎng)格應(yīng)用程序。
1. 打開DevExpress WinForms Data Grid的智能標(biāo)記,單擊Add Column來創(chuàng)建列。
2. 選擇此列并設(shè)置其屬性為唯一的字符串:“DiscountAmount”。
3. 將列的屬性設(shè)置為有效的數(shù)據(jù)類型。在本教程中,使用System.Decimal值。
1. 使用 屬性:單擊省略號按鈕打開Expression Editor(表達(dá)式編輯器)。
2. 創(chuàng)建一個簡單表達(dá)式,乘以三個字段:“Quantity”, “Unit Price”, “Discount”。
3. 將屬性設(shè)置為false來使該列只讀。
TIP:將列的設(shè)置為,將設(shè)置為c2,來將列值格式化為貨幣。
將列的屬性設(shè)置為true,來允許用戶在運行時修改未綁定列的表達(dá)式。
用戶可以在運行時從列的上下文菜單調(diào)用表達(dá)式編輯器來更改表達(dá)式。
1. 創(chuàng)建另一個列,設(shè)置為Total,為System.Decimal。
2. 選擇“gridView1”,并在屬性面板的“Events”頁面上訂閱事件。
注意:事件在每次即將顯示列值時和修改列單元格后(當(dāng)需要發(fā)布數(shù)據(jù)時)觸發(fā)。
3. 如果e.IsGetData事件參數(shù)為true,則使用方法檢索Quantity、UnitPrice和Discount列的值。計算未綁定列的值,并將其分配給e.Value事件參數(shù)。
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事件參數(shù)指示未綁定列中的單元格值是否被修改。
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
更多產(chǎn)品資訊及授權(quán),歡迎“”!
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@ke049m.cn
文章轉(zhuǎn)載自:慧都網(wǎng)