1
/// <summary>
2
/// 列得表达式求值
3
/// 孙二永20070612
4
/// </summary>
5
/// <param name="sender"></param>
6
/// <param name="e"></param>
7
private void Button2_Click(object sender, System.EventArgs e)
8
{
9
10
DataTable table = new DataTable ();
11
12
//创建table的第一列
13
DataColumn priceColumn = new DataColumn();
14
//该列的数据类型
15
priceColumn.DataType = System.Type.GetType("System.Decimal");
16
//该列得名称
17
priceColumn.ColumnName = "price";
18
//该列得默认值
19
priceColumn.DefaultValue = 50;
20
21
// 创建table的第二列
22
DataColumn taxColumn = new DataColumn();
23
taxColumn.DataType = System.Type.GetType("System.Decimal");
24
//列名
25
taxColumn.ColumnName = "tax";
26
//设置该列得表达式,用于计算列中的值或创建聚合列
27
taxColumn.Expression = "price * 0.0862";
28
29
30
// Create third column.
31
DataColumn totalColumn = new DataColumn();
32
33
totalColumn.DataType = System.Type.GetType("System.Decimal");
34
35
totalColumn.ColumnName = "total";
36
//该列的表达式,值是得到的是第一列和第二列值得和
37
totalColumn.Expression = "price + tax";
38
39
// 将所有的列添加到table上
40
table.Columns.Add(priceColumn);
41
table.Columns.Add(taxColumn);
42
table.Columns.Add(totalColumn);
43
44
//创建一行
45
DataRow row = table.NewRow();
46
47
//将此行添加到table中
48
table.Rows.Add(row);
49
50
//将table放在试图中
51
DataView view = new DataView(table);
52
53
dg.DataSource = view;
54
55
dg.DataBind();
56
57
58
59
60
61
62
}
63

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63
