Click here to Skip to main content
15,886,001 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on this code for a week and I couldn't solve this. My gridview has headertext with a textbox as a row. there r 2 rows with 4 columns. I couldn't able to convert rows to columns. Here is my code ...

C#
protected void Pageload()
{     
           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)));
                dr = dt.NewRow();
                dr["Color"] = string.Empty;
                dr["1 mois"] = string.Empty;
                dr["3 mois"] = string.Empty;
                dr["6 mois"] = string.Empty;
                dt.Rows.Add(dr);
                ViewState["CurrentTable"] = dt;
                DataTable dtnew=ConvertToColumns(dt);
                gvlistmonoprix.DataSource = dtnew;
                gvlistmonoprix.DataBind();

}
public DataTable ConvertToColumns(DataTable inputTable)
    {
        
        DataTable outputTable = new DataTable();

        // Add columns by looping rows

        // Header row's first column is same as in inputTable
        outputTable.Columns.Add(inputTable.Columns[0].ColumnName.ToString());

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

        // 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;
            }
            outputTable.Rows.Add(newRow);
            
        }
        return outputTable;
        
}
Posted
Updated 11-May-14 19:49pm
v2
Comments
Sriram Ramachandran 12-May-14 1:42am    
ASPX code
<asp:GridView ID="gvlistmonoprix" runat="server"
ShowFooter="True" AutoGenerateColumns="False"
CellPadding="4" ForeColor="Black"
GridLines="Horizontal" Width="640px"
ShowHeaderWhenEmpty="True" BackColor="White" BorderColor="#CCCCCC"
BorderStyle="None" BorderWidth="1px">

<columns>

<asp:TemplateField HeaderText="Color">
<itemtemplate>
<asp:TextBox ID="color" Width="100px" runat="server" TextMode="MultiLine">



<footerstyle horizontalalign="Left">
<footertemplate>
<asp:Button ID="ButtonAdd" runat="server" CssClass="blueButton1"
Text="Add New Color" ValidationGroup="null" OnClick="addnewrowmonoprix_click" />


<asp:TemplateField HeaderText="1 mois">
<itemtemplate>
<asp:TextBox ID="mois1" runat="server" OnTextChanged="calculatetotal_textchanged" AutoPostBack="true">



Nandakishore G N 12-May-14 2:02am    
Sriram. Can you explain the reason behind converting rows to columns?
Sriram Ramachandran 12-May-14 2:08am    
Actually the requirement is a gridview with 15 headertext followed by 15 textboxes in 1st row. Initially in the pageload, it displays the the gridview which goes out of the browser area(mean need to scroll right). And also I ve a add new row(need to convert to column) which duplicates the row for next entry. So, I need to change the rows to columns which displays the textbox in a vertical manner. The conversion was successful where I could see in debugging but not in the browser.
Sriram Ramachandran 12-May-14 2:14am    
Or I need to create a gridview with Rows manner instead of columns but the gridview doesn't supports rows
So I need to design in Columns and in pageload, I need to convert the Columns to Rows...
Sriram Ramachandran 12-May-14 2:23am    
@Nandakishore G N : Could you pls tell me what would be the issue behind this ??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900