16,554
社区成员
发帖
与我相关
我的任务
分享
Private Sub CalcColumns()
Dim rate As Single = .0862
Dim table As New DataTable()
' Create the first column.
Dim priceColumn As New DataColumn()
With priceColumn
.DataType = System.Type.GetType("System.Decimal")
.ColumnName = "price"
.DefaultValue = 50
End With
' Create the second, calculated, column.
Dim taxColumn As New DataColumn()
With taxColumn
.DataType = System.Type.GetType("System.Decimal")
.ColumnName = "tax"
.Expression = "price * 0.0862"
End With
' Create third column
Dim totalColumn As New DataColumn()
With totalColumn
.DataType = System.Type.GetType("System.Decimal")
.ColumnName = "total"
.Expression = "price + tax"
End With
' Add columns to DataTable
With table.Columns
.Add(priceColumn)
.Add(taxColumn)
.Add(totalColumn)
End With
Dim row As DataRow= table.NewRow
table.Rows.Add(row)
Dim view As New DataView
view.Table = table
DataGrid1.DataSource = view
End Sub
实现,主要用到 DataColumn.Expression属性.