1 添加FlexGrid控件
在vs 2005的ToolBox中单击鼠标右键,在弹出的快捷菜单中选择Choose Items
在弹出的【Choose ToolBox Items】对话框中,选中【Com Components】选项卡,在列表框中选择【Microsoft Flex Grid Control version 6.0】

单击【OK】,Microsoft FlexGrid控件则会添加到ToolBox中。

2 使用MSFlexGrid显示多维数据集合
private void DisplayCellSet(CellSet cs)
{
cellSetGrid.Redraw = false;
try
{
cellSetGrid.Clear();
AxisCollection axes = cs.Axes; //获取轴的集合
int axesCount = axes.Count;
if (axesCount > 2)
throw new Exception("This sample application do not support ");
Set columns = null;
Set rows = null;
int columncount = 0;
int rowcount = 1;
int fixedColumnCount = 0;
int fixedRowCount = 0;
int gridRow = 0;
int gridColumn = 0;
if (axesCount > 0)
{
columns = axes[0].Set;
columncount = columns.Tuples.Count;
fixedRowCount += columns.Hierarchies.Count; //固定行的个数
}
if (axesCount > 1)
{
rows = axes[1].Set;
rowcount = rows.Tuples.Count;
fixedColumnCount += rows.Hierarchies.Count; //固定列的个数
}
cellSetGrid.Cols = fixedColumnCount + columncount; //设置控件的列数
cellSetGrid.FixedCols = fixedColumnCount; //设置控件的固定的列数
cellSetGrid.Rows = fixedRowCount + rowcount; //设置控件的行数
cellSetGrid.FixedRows = fixedRowCount; //设置控件的可变的
gridColumn = fixedColumnCount;
//添加列标题
foreach (Tuple tuple in columns.Tuples)
{
gridRow = 0;
foreach (Member member in tuple.Members)
{
cellSetGrid.set_TextMatrix(gridRow, gridColumn, member.Caption);
gridRow++;
// cellSetGrid.set_ColWidth(1, 2000);
}
gridColumn++;
}
//添加行标题
if (rows != null)
{
gridRow = fixedRowCount;
foreach (Tuple tuple in rows.Tuples)
{
gridColumn = 0;
foreach (Member member in tuple.Members)
{
cellSetGrid.set_TextMatrix(gridRow, gridColumn, member.Caption);
gridColumn++;
}
gridRow++;
}
}
//填充数据
int iCell = 0;
for (gridRow = fixedRowCount; gridRow < cellSetGrid.Rows; gridRow++)
{
for (gridColumn = fixedColumnCount; gridColumn < cellSetGrid.Cols; gridColumn++)
{
if (cs.Cells[iCell].Value != null)
{
cellSetGrid.set_TextMatrix(gridRow, gridColumn, cs.Cells[iCell].Value.ToString());
}
iCell++;
}
}
//设置列宽
int maxwidth = 0;
for (gridColumn = 0; gridColumn < cellSetGrid.Cols; gridColumn++)
{
for (gridRow = 0; gridRow < cellSetGrid.Rows; gridRow++)
{
int actualWidth = cellSetGrid.get_TextMatrix(gridRow, gridColumn).Length * 100;
if (actualWidth > maxwidth)
maxwidth = actualWidth;
}
cellSetGrid.set_ColWidth(gridColumn, maxwidth);
}
}
catch(Exception err)
{
MessageBox.Show(err.Message);
}
finally
{
cellSetGrid.Redraw = true;
}
}
程序执行结果如下图所示:

文件下载路径:
http://files.cnblogs.com/sorosjing/CellGridDemo.rar