0

I've to do the transpose of my DataTable and I used the code I found in internet. This code is giving an extra row at the top showing the column index. How can I remove this row?This extra row is at above the header row of my DataTable, and when I use the code dt_.Rows.Remove(dt_.Rows[0]); the header row is removed but not the top row. Reference taken from: Transpose a datatable

How can I remove the top row?

DataTable dt_ = new DataTable();

DataTable table = new DataTable();
//Get all the rows and change into columns
for (int i = 0; i <= dt.Rows.Count; i++)
{
    table.Columns.Add(Convert.ToString(i));
}
DataRow dr;
//get all the columns and make it as rows
for (int j = 0; j < dt.Columns.Count; j++)
{
    dr = table.NewRow();
    dr[0] = dt.Columns[j].ToString();
    for (int k = 1; k <= dt.Rows.Count; k++)
    {
        dr[k] = dt.Rows[k - 1][j];
    }

    table.Rows.Add(dr);
}

dt_ = table;

//dt_.Rows.Remove(dt_.Rows[0]); removes the header row

My gridview code

<asp:GridView ID="GridView1"
                        runat="server"
                        CellPadding="3"
                        CellSpacing="2"
                        AutoGenerateColumns="true"
                        ShowFooter="true"
                        FooterStyle-HorizontalAlign="Left"
                        RowStyle-BorderColor="Black" HeaderStyle-BackColor="#0CA3D2">
                        <FooterStyle BackColor="#87CEFA" />
                    </asp:GridView>

I've not shown the footer section sum calculation code behind because it is irrelevant here.

snapshot:

enter image description here

user4221591
  • 2,084
  • 7
  • 34
  • 68

2 Answers2

0

I think if you start the loop from 1 it should work

        DataTable dt = new DataTable();
        DataTable dt_ = new DataTable();

        DataTable table = new DataTable();
        //Get all the rows and change into columns
        for (int i = 0; i <= dt.Rows.Count; i++)
        {
            table.Columns.Add(Convert.ToString(i));
        }
        DataRow dr;
        //get all the columns and make it as rows
        //HERE THE LOOK STARTS FROM ONE
        for (int j = 1; j < dt.Columns.Count; j++)
        {
            dr = table.NewRow();
            dr[0] = dt.Columns[j].ToString();
            for (int k = 1; k <= dt.Rows.Count; k++)
            {
                dr[k] = dt.Rows[k - 1][j];
            }

            table.Rows.Add(dr);
        }

        dt_ = table;
Yousuf Hossain
  • 172
  • 1
  • 12
0

That's because

//Get all the rows and change into columns
for (int i = 0; i <= dt.Rows.Count; i++)
{
    table.Columns.Add(Convert.ToString(i));
}

is creating columns named 0, 1, and 2. the problem is with Convert.ToString(i). That, and you will also need to change how you read k.

The code will look like this (not tested):

DataTable dt_ = new DataTable();

DataTable table = new DataTable();
//Get all the rows and change into columns
for (int i = 0; i <= dt.Rows.Count; i++)
{
    table.Columns.Add(dt.Rows[i][0]); // the column name is in the first cell
}
DataRow dr;
//get all the columns and make it as rows
for (int j = 0; j < dt.Columns.Count; j++)
{
    dr = table.NewRow();
    dr[0] = dt.Columns[j].ToString();

    // changed the for condition
    for (int k = 1; k < dt.Rows.Count; k++)
    {
        dr[k] = dt.Rows[k][j]; // changed the 'k - 1' to 'k'
    }

    table.Rows.Add(dr);
}

dt_ = table;
Balah
  • 2,530
  • 2
  • 16
  • 24