DataTable dt = new DataTable();
                DataRow dr = null;
                dt.Columns.Add(new DataColumn("Color", typeof(string)));
                dt.Columns.Add(new DataColumn("1 mois", typeof(string)));
                dt.Columns.Add(new DataColumn("3 mois", typeof(string)));
                dt.Columns.Add(new DataColumn("6 mois", typeof(string)));
                dt.Columns.Add(new DataColumn("9 mois", typeof(string)));
                dt.Columns.Add(new DataColumn("12 mois", typeof(string)));
                dt.Columns.Add(new DataColumn("18 mois", typeof(string)));
                dt.Columns.Add(new DataColumn("24 mois", typeof(string)));
                dt.Columns.Add(new DataColumn("36 mois", typeof(string)));
                dr = dt.NewRow();
                dr["Color"] = string.Empty;
                dr["1 mois"] = string.Empty;
                dr["3 mois"] = string.Empty;
                dr["6 mois"] = string.Empty;
                dr["9 mois"] = string.Empty;
                dr["12 mois"] = string.Empty;
                dr["18 mois"] = string.Empty;
                dr["24 mois"] = string.Empty;
                dr["36 mois"] = string.Empty;

                ViewState["CurrentTable"] = dt;
                DataTable dtnew=ConvertToColumns(dt);
                gvlistmonoprix.DataSource = dtnew;

         public DataTable ConvertToColumns(DataTable inputTable)

        DataTable outputTable = new DataTable();

        // Add columns by looping rows

        // Header row's first column is same as in inputTable

        // Header row's second column onwards, 'inputTable's first column taken
        foreach (DataRow inRow in inputTable.Rows)
            string newColName = inRow[0].ToString();

        // Add rows by looping columns        
        for (int rCount = 1; rCount <= inputTable.Columns.Count -1; rCount++)
            DataRow newRow = outputTable.NewRow();

            // First column is inputTable's Header row's second column
            newRow[0] = inputTable.Columns[rCount].ColumnName.ToString();
            for (int cCount = 0; cCount <= inputTable.Rows.Count - 1; cCount++)
                string colValue = inputTable.Rows[cCount][rCount].ToString();
                newRow[cCount + 1] = colValue;

        return outputTable;

Hi guys... The code above converts the rows into columns. I got the expected output when I make checkpoints in code but not in webpage. Did I miss something when binding the datatable to gridview? Could someone help me pls ???

This article has been dead for over six months. Start a new discussion instead.