{
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;
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;
sriram_1 0 Newbie Poster
sriram_1 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.