Friday, 10 August 2012

DATATABLE CROSS TAB

 public DataTable ConvertToCrossTab(DataTable table)
    {
        DataTable newTable = new DataTable();

        for (int r = 0; r <= table.Rows.Count; r++)
        {
            newTable.Columns.Add(r.ToString());
        }

        for (int c = 0; c < table.Columns.Count; c++)
        {
            DataRow row = newTable.NewRow();
            object[] rowValues = new object[table.Rows.Count + 1];

            rowValues[0] = table.Columns[c].Caption;

            for (int r = 1; r <= table.Rows.Count; r++)
            {
                rowValues[r] = table.Rows[r - 1][c].ToString();
            }

            row.ItemArray = rowValues;
            newTable.Rows.Add(row);
        }

        return newTable;
    }