Transpose a DataTable using DataSet (C#)

DataSet ds = new DataSet();
OracleDataAdapter dataAdapter = new OracleDataAdapter();
dataAdapter.SelectCommand=cmd;

DataAdapter.Fill(ds);

DataTableCollection collection = ds.Tables;
                        DataTable table=new DataTable();
                        for (int i = 0; i < collection.Count; i++)
                        {
                            table = collection[i];
                        }
DataTabel transposeTable = GetTransposeTable(table);


private DataTable GetTransposeTable(DataTable dtOld)
        {
            DataTable dtNew = new DataTable();

            dtNew.Columns.Add(new DataColumn("0", typeof(string)));
            for (int i = 0; i < dtOld.Columns.Count; i++)
            {
                DataRow newRow = dtNew.NewRow();
                newRow[0] = dtOld.Columns[i].ColumnName;
                for (int j = 1; j <= dtOld.Rows.Count; j++)
                {
                    if (dtNew.Columns.Count < dtOld.Rows.Count + 1)
                        dtNew.Columns.Add(new DataColumn(j.ToString(), typeof(string)));
                    newRow[j] = dtOld.Rows[j - 1][i];
                }
                dtNew.Rows.Add(newRow);
            }
            return dtNew;
        }

Refer for additional information : http://codemaverick.blogspot.com/2008/02/transpose-datagrid-or-gridview-by.html

Comments

Popular posts from this blog

SharePoint 2016 and 2019 Ports

Clear fields based on drop-down values in PowerApps

Hide a SharePoint list field based on user group using JQuery Webservice in 2010