Click here to Skip to main content
15,909,539 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("Col1", typeof(string)));
dt.Columns.Add(new DataColumn("Col2", typeof(string)));
dt.Columns.Add(new DataColumn("Col3", typeof(string)));
dt.Columns.Add(new DataColumn("Col4", typeof(string)));
dr = dt.NewRow();
dr["Col1"] = string.Empty;
dr["Col2"] = string.Empty;
dr["Col3"] = string.Empty;
dr["Col4"] = string.Empty;
C#
dr = dt.NewRow();
                dr["Col1"] = string.Empty;
                dr["Col2"] = string.Empty;
                dr["Col3"] = string.Empty;
                dr["Col4"] = string.Empty;

dr = dt.NewRow();
ViewState["CurrentTable"] = dt;

On page load, I used to load 2 rows in gridview. The gridview is bound with the BoundField..
I need to convert the 2 rows to columns wise. Surfed many websites but no changes are happening.... Can anyone share me the apt website to convert rows to columns....
Posted

1 solution

 
Share this answer
 
Comments
Sriram Ramachandran 9-May-14 2:42am    
I got the output as expected when I use Checkpoint and see the output in Datasource...But the output in browser its different .... Don't no whats happening??
Sriram Ramachandran 9-May-14 6:09am    
Friends Someone help me ... I couldn't find the problem....
Sriram Ramachandran 9-May-14 6:17am    
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;


Is this the code wrong? Got output when I make cheeckpoints in coding but not in output screen ??

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