0

I have a datatable with many columns, like so;

Name | Age | Foo | Bar | Etc..
Joe | 30 | foo | bar | Etc..

I need to now get this datatable into just two columns consisting of just the headers followed by their values, like so;

Header | Value
Name | Joe
Age | 30
Foo | foo
Etc... | etc..

This is so that a DataBinder can access it easily when the data is bound.

How can this be done, and do I need to do this at all?

EDIT: Including example code

asp.net

<div class="itemDetail col-s">                                                                                                                       
  <div class="itemDetailLabel">                                                                                                                            
    <%# DataBinder.Eval(Container.DataItem, "Caption")%>                                                                                                                        
  </div>                                                                                                                        
  <div class="itemDetailValue">                                                                                                                       
    <%# If(DataBinder.Eval(Container.DataItem, "UIValue") Is Nothing OrElse String.IsNullOrEmpty(DataBinder.Eval(Container.DataItem, "UIValue").ToString()), "&nbsp;", DataBinder.Eval(Container.DataItem, "UIValue"))   %>                                                                                                                        
  </div>                                                                                                                    
</div>

vb.net

Dim oldDataTable As DataTable = DataSource ''Which is a datatable of a single row
Dim newDataTable As New DataTable

newDataTable.Columns.Add("Caption")
newDataTable.Columns.Add("UIValue")

''Change the single row into multiple rows

fooControl.DataSource = newDataTable
fooControl.DataBind()

EDIT2: So for those that are still learning like me, this is called transposing. Thanks for @gofal3 for pointing it out

1 Answers1

0

You have to transpose the DataTable. As far as I know there is no built in method, but you have to write your own code to do so. But there are a lot of examples how other did it. eg: https://www.codeproject.com/Articles/44274/Transpose-a-DataTable-using-C or Transpose a datatable

gofal3
  • 1,213
  • 10
  • 16
  • Ah okay, I didn't know there was a name for it, which is probably why my searching wasn't returning anything of value. – Alex Wilson May 14 '18 at 10:35