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

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.