views:

1559

answers:

4

I have 10 records of data in a DataTable which has 3 fields "Foo", "Bar", and "Baz".

If I connect wire this into a DataGridView I see 10 rows and 3 columns, and the column headers display the names of the fields.

I'm wondering how easy it is to reverse the rows and columns so that with the same data I end up with 3 rows and 10 columns, with field names being displayed in the row headers.


I can do some things manualy, like overriding an OnPaint method and drawing the field names directly into the row header cells, but I'm looking for something more automated.

Again, there's a suggestion of swapping values manualy, but unless I make all values Strings, this isn't going to work. 3 columns in a data table - Foo, Bar, and Baz of types int, float and string just won't transpose.

Even if I managed all of these manual changes, my data grids have CheckBox columns, ComboBox columns - no such row counterpart exists - there is no CheckBox row or ComboBox row. Where I currently just need to tell the compiler to "Add a ComboBoxColumn" I'd have to re-write so that every cell was generated individually.

Ideally I'd like a TransposableDataGridView which exposed all functionality of the DataGridView, with an additional bool property "Transposed". That way I could leave all of my code exactly as it is - I wouldn't have to change anything except for the type of the grid.

If nothing like this exists, I might just have to go and write it. (Should only take me a year or so! :)

A: 

You can do this by programmatically adding the number of columns you need (e.g., 7 more to make 10) then programmatically swapping row, col with col, row.

Arnshea
Are you suggesting add columns to the DataTable or to the DataGridView?
Tim Gradwell
+1  A: 

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;
svick
Thanks for the answer - what happens if I have an bool DataColumn and a string DataColumn - what type do I make the columns of the transposed grid?
Tim Gradwell
If you don't set the type, it defaults to string, so if you just want to show the data to the user that's fine for you, because values of other types will be automatically converted. The other possibility is to set the type to object. Variable of this type can contain anything.
svick
A: 

This code should do the trick http://aspalliance.com/538_CodeSnip_Pivot_Tables_with_ADONET_and_Display_in_a_DataGrid_Paged_Horizontally

The display is asp.net but you can bind the resulting datatable to datagridview and get the result you want

Kumar
+1  A: 

I use the Developer Expres Vertical Grid.. It is like you want a rotated grid. It also allows for a different editor per row.

Link to vertical grid product page

Julian de Wit