hi eveyone

i have to big problem so

this problem have two datatable

first table like

column1----column2----column3
row11-----row21-----row31
row12-----row22-----row32

i want to inset to other table

column1---row11
column1---row12
column2---row21
column2---row22
column3---row31
column3---row32

my code:

                DataTable orjiTable = new DataTable();
                DataTable gecicitable = new DataTable();


                adptr.Fill(gecicitable);







          //    orjiTable.Columns.Add(gecicitable.Columns[0].ColumnName.ToString());
             // MessageBox.Show(gecicitable.Columns[0].ColumnName.ToString());

              foreach (DataRow inRow in gecicitable.Rows)
              {



                  string newColName = inRow[0].ToString();

                  MessageBox.Show(newColName); 
                  orjiTable.Columns.Add(newColName); 
                  DataRow newRow1 = orjiTable.NewRow();
                  string den = gecicitable.Columns[0].ColumnName.ToString();
                  newRow1[0] = den;
                  orjiTable.Rows.Add(den);



              }



               for (int rCount = 1; rCount <= gecicitable.Columns.Count - 1; rCount++)
               {
                   DataRow newRow = orjiTable.NewRow();

                   newRow[0] = gecicitable.Columns[rCount].ColumnName.ToString();

                   MessageBox.Show(gecicitable.Columns[rCount].ColumnName.ToString());

                   for (int cCount = 0; cCount <= gecicitable.Rows.Count - 1; cCount++)
                   {

                       string colValue = gecicitable.Rows[cCount][rCount].ToString();

                       newRow[cCount + 2] = colValue;

                       MessageBox.Show(colValue);

                   }

                   orjiTable.Rows.Add(newRow);

               }

when runnig this code output :

row11--------row12
column1
column1
row21--------row22
row31--------row32

input taple
column1-----column2-----column3
row11--------row21-------row31
row12--------row22-------row32

i want to like this output:

column1---- row11
column1---- row12
column2---- row21
column2---- row22
column3---- row31
column3---- row32

Dani AI

Generated

This transform is the classic “unpivot”: turn each cell into a pair (original column name, cell value). was on the right track with a two‑column destination table — the real issue in the samples is how rows are created and populated. The OP was adding column names and values as separate rows instead of adding both values into one DataRow, and a missing index increment caused rows to be overwritten or placed incorrectly.

A concise, safe approach is to flatten the source with LINQ and then materialize the result table. This avoids manual counter mistakes and makes DBNull handling explicit:

// source: DataTable src
var flat = src.Columns.Cast<DataColumn>()
    .SelectMany(col => src.AsEnumerable()
        .Select(r => new {
            Column = col.ColumnName,
            Value  = r.IsNull(col) ? "" : r[col].ToString()
        }));

var dst = new DataTable();
dst.Columns.Add("Column", typeof(string));
dst.Columns.Add("Value",  typeof(string));

foreach (var item in flat)
    dst.Rows.Add(item.Column, item.Value);

If you prefer plain loops, follow these rules: 1) Create the destination columns first. 2) For every source column and for every source row create one DataRow, set both fields (column name and cell value), then call Rows.Add(newRow). Don’t call Rows.Add(value) repeatedly — that adds separate rows instead of filling columns. For large tables wrap inserts with BeginLoadData/EndLoadData for speed, and preserve original data types if you need non‑string values.

With that fix the output will be exactly the sequence you want (column1,row11; column1,row12; column2,row21; ...). Replace MessageBox debugging with Debug.WriteLine or breakpoints while testing.

Recommended Answers

All 3 Replies

Try this:

DataTable table2 = new DataTable();
table2.Columns.Add("Column_name", typeof(string));
table2.Columns.Add("Row_name", typeof(string));

DataRow row;
int counter = 0;
for(int i = 0; i < tableOriginal.Columns.Count; i++) //loop through the columns of original table
{

    for(int j = 0; j < tableOriginal.Rows.Count; j++)
    {        
        if(j == 0)
        {
            table2.Rows.Add();
            table2.Rows[counter][0] = tableOriginal.Rows[j][i];
        }
        else if(j == 1)
            table2.Rows[counter][1] = tableOriginal.Rows[j][i];
    }
}   

Maybe there will some any error, I did the code by heart. Let me know if its working.
bye

i changed your code

we nearly to answer :)

it is :

  for (int i = 0; i < gecicitable.Columns.Count; i++) 
                {

                  //  MessageBox.Show(gecicitable.Columns[i].ToString());


                    for (int j = 0; j < gecicitable.Rows.Count; j++)
                    {
                       //
                        if (j == 0)
                        {

                            orjitable.Rows.Add(gecicitable.Columns[i].ToString());
                            orjitable.Rows.Add(gecicitable.Rows[j][i].ToString());
                            MessageBox.Show(gecicitable.Rows[j][i].ToString());
                        }

                         if (j == 1)
                        {
                             orjitable.Rows.Add(gecicitable.Columns[i].ToString());
                             orjitable.Rows.Add(gecicitable.Rows[j][i].ToString());
                             MessageBox.Show(gecicitable.Rows[j][i].ToString());
                        }
                    }

                }

output :

column1
row11
column1
row12
column2
row21
column2
row22
columns3
row31
columns3
row32

i want like output ;

column1---- row11
column1---- row12
column2---- row21
column2---- row22
column3---- row31
column3---- row32

where is wrong

anyone else?

Sorry, I forgot o enumerate my "couner" variable. So it moves to a new row every 2nd itteration:

DataTable table2 = new DataTable();
table2.Columns.Add("Column_name", typeof(string));
table2.Columns.Add("Row_name", typeof(string));

DataRow row;
int counter = 0;
for(int i = 0; i < tableOriginal.Columns.Count; i++) //loop through the columns of original table
{

    for(int j = 0; j < tableOriginal.Rows.Count; j++)
    {        
        if(j == 0)
        {
            table2.Rows.Add();
            table2.Rows[counter][0] = tableOriginal.Rows[j][i];
        }
        else if(j == 1)
            table2.Rows[counter++][1] = tableOriginal.Rows[j][i]; //here I do +1, so it will write to the right row
    }

}

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.