在ADOMD.NET下返回的CellSet结果通常来说是无法直接用到容器控件当中的,因为其并没有实现相应的接口,所以通常需要转换成DataTable然后再做处理。
代码
1 public DataTable ToDataTable(CellSet cs)
2 {
3 DataTable dt = new DataTable();
4 dt.TableName = "resulttable";
5 DataColumn dc = new DataColumn();
6 DataRow dr = null;
7
8 //第一列:必有为维度描述(行头)
9 dt.Columns.Add(new DataColumn("Description"));
10
11 //生成数据列对象
12 string name;
13
14 foreach (Position p in cs.Axes[0].Positions)
15 {
16 dc = new DataColumn();
17 name = "";
18 foreach (Member m in p.Members)
19 {
20 name = name + m.Caption + " ";
21 }
22
23 dc.ColumnName = name;
24 dt.Columns.Add(dc);
25 }
26
27 //添加行数据
28 int pos = 0;
29
30 foreach (Position py in cs.Axes[1].Positions)
31 {
32 dr = dt.NewRow();
33
34 //维度描述列数据(行头)
35 name = "";
36
37 foreach (Member m in py.Members)
38 {
39 name = name + m.Caption + "\r\n";
40 }
41 dr[0] = name;
42
43 //数据列
44 for (int x = 1; x <= cs.Axes[0].Positions.Count; x++)
45 {
46 dr[x] = cs[pos++].FormattedValue;
47 }
48 dt.Rows.Add(dr);
49 }
50 return dt;
51 }
2 {
3 DataTable dt = new DataTable();
4 dt.TableName = "resulttable";
5 DataColumn dc = new DataColumn();
6 DataRow dr = null;
7
8 //第一列:必有为维度描述(行头)
9 dt.Columns.Add(new DataColumn("Description"));
10
11 //生成数据列对象
12 string name;
13
14 foreach (Position p in cs.Axes[0].Positions)
15 {
16 dc = new DataColumn();
17 name = "";
18 foreach (Member m in p.Members)
19 {
20 name = name + m.Caption + " ";
21 }
22
23 dc.ColumnName = name;
24 dt.Columns.Add(dc);
25 }
26
27 //添加行数据
28 int pos = 0;
29
30 foreach (Position py in cs.Axes[1].Positions)
31 {
32 dr = dt.NewRow();
33
34 //维度描述列数据(行头)
35 name = "";
36
37 foreach (Member m in py.Members)
38 {
39 name = name + m.Caption + "\r\n";
40 }
41 dr[0] = name;
42
43 //数据列
44 for (int x = 1; x <= cs.Axes[0].Positions.Count; x++)
45 {
46 dr[x] = cs[pos++].FormattedValue;
47 }
48 dt.Rows.Add(dr);
49 }
50 return dt;
51 }