Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
I am Having Grid with columns as sample,al,cr,mg,etc and rows as 1307,.5,.5,.25,
1308,0.2,0.3,0.5 etc
how can interchange it with coulms as
sample ,elements,values
                                      1307   ,al,        05
                                       1307,  cr,     0.5
                                       ------------------
                                      1308,al,     0.5
etc
 
iam using just a DataTable
my DataTable is
 DataTable dt = new DataTable();
            dt.Columns.Add("Sample", Type.GetType("System.Int32"));
            dt.Columns.Add("Al", Type.GetType("System.String"));
            dt.Columns.Add("Cr", Type.GetType("System.String"));
            dt.Columns.Add("Cu", Type.GetType("System.String"));
            dt.Columns.Add("Fe", Type.GetType("System.String"));
            dt.Columns.Add("Pb", Type.GetType("System.String"));
            dt.Columns.Add("Sn", Type.GetType("System.String"));
            dt.Columns.Add("B", Type.GetType("System.String"));
            dt.Columns.Add("Na", Type.GetType("System.String"));
            dt.Columns.Add("Si", Type.GetType("System.String"));
            dt.Columns.Add("Li", Type.GetType("System.String"));
            dt.Columns.Add("Mo", Type.GetType("System.String"));
            dt.Columns.Add("Ni", Type.GetType("System.String"));
            dt.Columns.Add("Ti", Type.GetType("System.String"));
            dt.Columns.Add("Ag", Type.GetType("System.String"));
            dt.Columns.Add("Mn", Type.GetType("System.String"));
            dt.Columns.Add("V", Type.GetType("System.String"));
            dt.Columns.Add("Ca", Type.GetType("System.String"));
            dt.Columns.Add("P", Type.GetType("System.String"));
            dt.Columns.Add("Zc", Type.GetType("System.String"));
            dt.Columns.Add("Mg", Type.GetType("System.String"));
            dt.Columns.Add("Cd", Type.GetType("System.String"));
            dt.Rows.Add(new object[] { 1304017, -0.184, -1.377, -0.767, -0.830, -1.143, -9.130, -0.389, -0.715, -0.393, -0.865, -0.951, -0.831, -1.259, 0.167, 1.128, -0.931, -7.90, 0.110, -1.133, -1.248, -1.133 });
            dt.Rows.Add(new object[] { 1304018, -0.255, -1.094, -1.094, 12.450, -0.442, -8.583, 1.922, 0.351, -0.393, -0.530, -0.6, -0.8, -0.259, 0.16, -0.679, -0.931, -7.90, 0.110, -1.133, -1.248, -0.679 });
Posted 19-Apr-13 1:13am
Edited 19-Apr-13 2:05am
v2
Comments
Maciej Los at 19-Apr-13 6:23am
   
Not clear. Please, be more specific and provide more details.
arthamsai at 19-Apr-13 6:46am
   
i want to inter change rows to columns and columns to rows
the present grid is
Sample Al Cr Mg
1307 0.5 .6 0.5
but i want out put as
sample Elements Value
1307 Al 0.6
1307 Cr 0.5
Maciej Los at 19-Apr-13 6:48am
   
OK, but what kind of database: SQL Server, MS Access, MySQL, Postgree?
arthamsai at 19-Apr-13 6:50am
   
iam using just a DataTable
my DataTable is
DataTable dt = new DataTable();
dt.Columns.Add("Sample", Type.GetType("System.Int32"));
dt.Columns.Add("Al", Type.GetType("System.String"));
dt.Columns.Add("Cr", Type.GetType("System.String"));
dt.Columns.Add("Cu", Type.GetType("System.String"));
dt.Columns.Add("Fe", Type.GetType("System.String"));
dt.Columns.Add("Pb", Type.GetType("System.String"));
dt.Columns.Add("Sn", Type.GetType("System.String"));
dt.Columns.Add("B", Type.GetType("System.String"));
dt.Columns.Add("Na", Type.GetType("System.String"));
dt.Columns.Add("Si", Type.GetType("System.String"));
dt.Columns.Add("Li", Type.GetType("System.String"));
dt.Columns.Add("Mo", Type.GetType("System.String"));
dt.Columns.Add("Ni", Type.GetType("System.String"));
dt.Columns.Add("Ti", Type.GetType("System.String"));
dt.Columns.Add("Ag", Type.GetType("System.String"));
dt.Columns.Add("Mn", Type.GetType("System.String"));
dt.Columns.Add("V", Type.GetType("System.String"));
dt.Columns.Add("Ca", Type.GetType("System.String"));
dt.Columns.Add("P", Type.GetType("System.String"));
dt.Columns.Add("Zc", Type.GetType("System.String"));
dt.Columns.Add("Mg", Type.GetType("System.String"));
dt.Columns.Add("Cd", Type.GetType("System.String"));
dt.Rows.Add(new object[] { 1304017, -0.184, -1.377, -0.767, -0.830, -1.143, -9.130, -0.389, -0.715, -0.393, -0.865, -0.951, -0.831, -1.259, 0.167, 1.128, -0.931, -7.90, 0.110, -1.133, -1.248, -1.133 });
dt.Rows.Add(new object[] { 1304018, -0.255, -1.094, -1.094, 12.450, -0.442, -8.583, 1.922, 0.351, -0.393, -0.530, -0.6, -0.8, -0.259, 0.16, -0.679, -0.931, -7.90, 0.110, -1.133, -1.248, -0.679 });
Maciej Los at 19-Apr-13 7:30am
   
Please, next time, use "Improve question" widget.
arthamsai at 19-Apr-13 7:47am
   
With the above datatable values i want the out not manully
arthamsai at 19-Apr-13 7:50am
   
Iam Using this Code
 

public static DataTable GetInvertedTable(DataTable dt, string column, params string[] columnsToIgnore)
{
DataTable dataTable = new DataTable();
//if (column == "Elements")
//{
// column = dt.Columns[0].ColumnName;
//}
dataTable.Columns.Add("Sample");
//dataTable.Columns.Add("");
dataTable.Columns.Add("Elements");
dataTable.Columns.Add("Result");
List columns = new List();
List listColumnsToIgnore = new List();
if (columnsToIgnore.Length > 0)
listColumnsToIgnore.AddRange(columnsToIgnore);
 
if (!listColumnsToIgnore.Contains(column))
listColumnsToIgnore.Add(column);
//foreach (DataRow dr in dt.Rows)
//{
// ////string coulmn = dr[dataTable.Columns.Add("Elements")].ToString();
// //if (!columns.Contains())
// //{
// // columns.Add(coulmn);
// // dataTable.Columns.Add(coulmn);
// //}
//}
foreach (DataColumn dc in dt.Columns)
{
if (!columns.Contains(dc.ColumnName) && !listColumnsToIgnore.Contains(dc.ColumnName))
{
DataRow dr = dataTable.NewRow();
dr[0] = (dc.ColumnName).ToString();
dr[1] = dc.ColumnName.ToString();
dr[2] = (dc.ColumnName);
//dr[1] = dc.ColumnName;
dataTable.Rows.Add(dr);
}
}
for (int i = 0; i < dataTable.Rows.Count; i++)
{
for (int j = 1; j < dataTable.Columns.Count; j++)
{
dataTable.Rows[i][j] =
dt.Rows[j - 1][dataTable.Rows[i][0].ToString()].ToString();
}
}
 
return dataTable;
 
}

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Please, have a look at your code:
dt.Columns.Add("Sample", Type.GetType("System.Int32"));
dt.Columns.Add("Al", Type.GetType("System.String"));
dt.Columns.Add("Cr", Type.GetType("System.String"));
...
 dt.Rows.Add(new object[] { 1304017, -0.184, -1.377, -0.767, -0.830, -1.143, -9.130, -0.389, -0.715, -0.393, -0.865, -0.951, -0.831, -1.259, 0.167, 1.128, -0.931, -7.90, 0.110, -1.133, -1.248, -1.133 });
All columns in datatable, except Sample) have been declared as String, but what kind of values are there stored?
-0.184 is not equal to "-0.184", because first is a numeric value and second is a string.
 
If you want to "manually" pivot, you need to set datatable object as follow:
dt.Columns.Add("Sample", Type.GetType("System.Int32"));
dt.Columns.Add("ElementDescription", Type.GetType("System.String"));
dt.Columns.Add("ElementValue", Type.GetType("System.Double"));
//then
dt.Rows.Add(new object[] { 1304017, "Al", -0.184));
 
See msdn documentation: DataTable[^]
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 406
1 Marcin Kozub 225
2 Sergey Alexandrovich Kryukov 205
3 Raul Iloc 170
4 Maciej Los 164
0 OriginalGriff 8,289
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,624
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 19 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100