Friday, May 27, 2011

Converting AdomdClient.CellSet to Ado DataSet

private DataTable ConvertCellSetToDataSet(CellSet cs)
{
DataTable dt = new DataTable();
DataColumn dc = null;
DataRow dr = null;

//add the columns
//
//first column
dt.Columns.Add(new DataColumn(" "));
string name = null;
StringBuilder builder = null;
//get the other columns from axis
foreach (var pos in cs.Axes[0].Positions)
{
dc = new DataColumn();
builder = new StringBuilder();
foreach (var mem in pos.Members)
{
// Decide how you want to display your colum and row headers when multiple dimensions to be displayed. The sample code appends a : between columns and rows
builder.AppendFormat("{0} : ", mem.Caption);
}
if (builder.Length > 0)
builder.Length -= 5;

dc.ColumnName = builder.ToString();
dt.Columns.Add(dc);
}

//add each row, row label first, then data cells
int y = 0;
foreach (var py in cs.Axes[1].Positions)
{
dr = dt.NewRow();
//create new row
// Do the row label
builder = new StringBuilder();
foreach (var mem in py.Members)
{
builder.AppendFormat("{0} : ", mem.Caption);
}
if (builder.Length > 0)
builder.Length -= 5;

dr[0] = builder.ToString();
//first cell in the row

// Data cells
int x = 0;
for (x = 0; x <= cs.Axes[0].Positions.Count - 1; x++)
{
dr[x + 1] = cs[x, y].FormattedValue;
//other cells in the row
}

dt.Rows.Add(dr);
//add the row
y = y + 1;
}

return dt;
}

1 comment:

Vankayala said...

I tried your code. Small correction: if (builder.Length > 0)
builder.Length -= 5;

Change to 3;