Rabu, 22 Desember 2010

Example - Implement Unbound Columns Via Event

Assume that the XtraGrid is bound to the [Order Details] table in the NWind database. The grid contains "Quantity", "UnitPrice" and "Discount" columns which are bound to the corresponding fields in the database table. The example below shows how to add an unbound column to the grid to display the amount of each order according to the expression: Quantity*UnitPrice*(1-Discount).
The result is displayed below:

For another example which illustrates working with unbound columns, see the Unbound Columns tutorial.
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid.Columns

Private Sub Form1_Load(ByVal sender As System.Object, _
  ByVal e As System.EventArgs) Handles MyBase.Load
   ' ...

   ' Create an unbound column.
   Dim unbColumn As GridColumn = GridView1.Columns.AddField("Total")
   unbColumn.VisibleIndex = GridView1.Columns.Count
   unbColumn.UnboundType = DevExpress.Data.UnboundColumnType.Decimal
   ' Disable editing.
   unbColumn.OptionsColumn.AllowEdit = False
   ' Specify format settings.
   unbColumn.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric
   unbColumn.DisplayFormat.FormatString = "c"
   ' Customize the appearance settings.
   unbColumn.AppearanceCell.BackColor = Color.LemonChiffon
End Sub

' Returns the total amount for a specific row.
Private Function getTotalValue(ByVal view As ColumnView, _
  ByVal listSourceRowIndex As Integer) As Decimal
    Dim row As DataRow = NwindDataSet.Tables("Order Details").Rows(listSourceRowIndex)
    Dim unitPrice As Decimal = Convert.ToDecimal(row("UnitPrice"))
    Dim quantity As Decimal = Convert.ToDecimal(row("Quantity"))
    Dim discount As Decimal = Convert.ToDecimal(row("Discount"))
    Return unitPrice * quantity * (1 - discount)
End Function

' Provides data for the Total column.
Private Sub GridView1_CustomUnboundColumnData(ByVal sender As Object, _
  ByVal e As CustomColumnDataEventArgs) Handles GridView1.CustomUnboundColumnData
   If e.Column.FieldName = "Total" And e.IsGetData Then e.Value = _
     getTotalValue(CType(sender, ColumnView), e.ListSourceRowIndex)
End Sub
Example - Implement Unbound Columns Using Expressions

The following code shows how to create an unbound column (Ext Price) and populate it with data using expressions. Data for this column is calculated according to the formula: [Quantity] * [UnitPrice] * (1 - [Discount]), which is set via the GridColumn.UnboundExpression property. 

Imports DevExpress.XtraGrid.Columns

Dim columnExtPrice As GridColumn = New GridColumn()
columnExtPrice.FieldName = "ExtPrice"
columnExtPrice.Caption = "Ext Price"
columnExtPrice.UnboundType = DevExpress.Data.UnboundColumnType.Decimal
columnExtPrice.UnboundExpression = "[Quantity] * [UnitPrice] * (1 - [Discount])"
columnExtPrice.VisibleIndex = 0

Tidak ada komentar:

Posting Komentar