Is it possible to switch rows and columns in a datagridview?

You can create new DataTable, add appropriate number of collumns and then copy values from one table to the other, just swap rows and colums.

I don’t think you can set row header in the same way you can set column header (or at least I don’t know how), so you can put the field names in separate colum.

DataTable oldTable = new DataTable();

...

DataTable newTable = new DataTable();

newTable.Columns.Add("Field Name");
for (int i = 0; i < oldTable.Rows.Count; i++)
    newTable.Columns.Add();

for (int i = 0; i < oldTable.Columns.Count; i++)
{
    DataRow newRow = newTable.NewRow();

    newRow[0] = oldTable.Columns[i].Caption;
    for (int j = 0; j < oldTable.Rows.Count; j++)
        newRow[j+1] = oldTable.Rows[j][i];
    newTable.Rows.Add(newRow);
}

dataGridView.DataSource = newTable;

Leave a Comment