How to display rows as columns in DataGridView?

I am trying to display a dataset that was retrieved from a sql database using datagridview in VS 2008. But I need to display the data vertically, not horizontally. This is what I did at the beginning.

SqlCommand cmd = new SqlCommand("proc_SearchProfile", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@scute_id", SqlDbType.VarChar, (10)).Value = val;
SqlDataAdapter adapt = new SqlDataAdapter(cmd);
DataSet dset = new DataSet();

adapt.Fill(dset, "Profile");
this.dataGridView1.DataSource = dset;
this.dataGridView1.DataMember = "Profile";


I've searched and read several threads but none of them work. Can anyone help me to display the received data vertically in datagridview?


source to share

1 answer

Try the following:

var tbl = dset.Tables["Profile"]:
var swappedTable = new DataTable();
for (int i = 0; i <= tbl.Rows.Count; i++)
for (int col = 0; col < tbl.Columns.Count; col++)
    var r = swappedTable.NewRow();
    r[0] = tbl.Columns[col].ToString();
    for (int j = 1; j <= tbl.Rows.Count; j++)
        r[j] = tbl.Rows[j - 1][col];

dataGridView1.DataSource = swappedTable;




All Articles