Exemplo n.º 1
0
    public DataTable Pivot(
        DataTable src,
        string VerticalColumnName,
        string HorizontalColumnName,
        string ValueColumnName)
    {
        DataTable dst = new DataTable();

        if (src == null || src.Rows.Count == 0)
        {
            return(dst);
        }

        // find all distinct names for column and row
        ArrayList ColumnValues = new ArrayList();
        ArrayList RowValues    = new ArrayList();

        foreach (DataRow dr in src.Rows)
        {
            // find all column values
            object column = dr[VerticalColumnName];
            if (!ColumnValues.Contains(column))
            {
                ColumnValues.Add(column);
            }

            //find all row values
            object row = dr[HorizontalColumnName];
            if (!RowValues.Contains(row))
            {
                RowValues.Add(row);
            }
        }

        ColumnValues.Sort();
        RowValues.Sort();

        //create columns
        dst = new DataTable();
        dst.Columns.Add(VerticalColumnName, src.Columns[VerticalColumnName].DataType);
        Type t = src.Columns[ValueColumnName].DataType;

        foreach (object ColumnNameInRow in RowValues)
        {
            dst.Columns.Add(ColumnNameInRow.ToString(), t);
        }

        //create destination rows
        foreach (object RowName in ColumnValues)
        {
            DataRow NewRow = dst.NewRow();
            NewRow[VerticalColumnName] = RowName.ToString();
            dst.Rows.Add(NewRow);
        }

        //fill out pivot table
        foreach (DataRow drSource in src.Rows)
        {
            object key             = drSource[VerticalColumnName];
            string ColumnNameInRow = Convert.ToString(drSource[HorizontalColumnName]);
            int    index           = ColumnValues.IndexOf(key);
            dst.Rows[index][ColumnNameInRow] = sum(dst.Rows[index][ColumnNameInRow], drSource[ValueColumnName]);
        }

        return(dst);
    }
Exemplo n.º 2
0
 public string ConvertCoordinatesToShapeName(Vertex[] vertices)
 {
     ColNumber = SetColumnNumber();
     RowName   = SetRowName();
     return(RowName.ToString() + ColNumber);
 }