title: Adding dynamically created columns and rows to listview wpf
body: <p>I'm trying to add dynamic columns and dynamic rows to a listview. I just can't get why this is so freaking hard to do in wpf. So the story is as follows, I have the user select some tables from a SQL database, for instance Table: PgPoints and PgValuesDouble.
  So the listview should display it's column headers as PgPoints and PgValuesDouble. </p>
  
  <p>Next from the SQLDatabase the data is grabbed on the name of the selected tables (PgPoints and PgValuesDouble). This data is returned as a List. </p>
  
  <p>I have the following C# code right now:</p>
  
  <pre><code>  public partial class Page3 : Page
  {
  
      private List&lt;string&gt; columns = new List&lt;string&gt;();
  
      /*Variables passed through constructor*/
      private dbConnection connection;
      private List&lt;string&gt; selectedTables;
  
  
      public Page3(dbConnection connection, List&lt;string&gt; selectedTables)
      {
          InitializeComponent();
          this.connection = connection;
          this.selectedTables = selectedTables;
          init();
      }
  
      private void init()
      {
          gridView.Columns.Clear();
  
          //Loop through selected tables and add them as column names to listview
          foreach(string selectedTableNames in selectedTables)
          {
              GridViewColumn clm = new GridViewColumn();
              clm.Header = selectedTableNames;
              clm.Width = 100;
              gridView.Columns.Add(clm);
  
              //Loop through the data grabbed from the SQL database
              foreach (string columNames in connection.GetColumnsList(selectedTableNames))
              {
                  columnListView.Items.Add(columNames);
              }
          }
      }
  
      private void button_Click(object sender, RoutedEventArgs e)
      {
          Button b = (Button)sender;
  
          if (b.Equals(button_back))
          {
              NavigationService.GoBack();
          }
          else
          {
              //Go forward event here!
          }
      }
  }
  </code></pre>
  
  <p>And XAML:</p>
  
  <pre><code>&lt;Page x:Class="HCCSVExport.Pages.Page3"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:local="clr-namespace:HCCSVExport.Pages"
    mc:Ignorable="d" 
    d:DesignHeight="300" d:DesignWidth="300"
    Title="Page3" MinWidth="519" MinHeight="321"&gt;
  
  &lt;Grid x:Name="grid" Background="White"&gt;
      &lt;Button x:Name="button_next" Content="volgende" Margin="0,0,10,10" VerticalAlignment="Bottom" Click="button_Click" HorizontalAlignment="Right" Width="86.493"/&gt;
      &lt;Button x:Name="button_back" Content="vorige" Margin="0,0,101,10" VerticalAlignment="Bottom" Click="button_Click" HorizontalAlignment="Right" Width="89" Uid="button_back"/&gt;
      &lt;Label x:Name="label" Content="Selecteer de kolommen uit de tabellen" Margin="10,10,10,0" VerticalAlignment="Top" FontSize="16" HorizontalContentAlignment="Center"/&gt;
      &lt;ListView x:Name="columnListView" HorizontalAlignment="Left" Height="240" Margin="10,46,0,0" VerticalAlignment="Top" Width="499"&gt;
          &lt;ListView.View&gt;
              &lt;GridView x:Name="gridView"/&gt;
          &lt;/ListView.View&gt;
      &lt;/ListView&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>The result is that all the data that is grabbed from the SQL database is put in to both columns. How can I get the data that belongs to the PgPoints column under the PgPoints column and the data that belongs to the PgValuesDouble column under the PgValuesDouble column?</p>
  
  <p><a href="http://afbeelding.im/fDTAd4ta" rel="nofollow">Screenshot</a></p>
  
  <p>As you can see the cells in both columns are the same instead of different cells for each column...</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>So i've changed to a datagrid which seems to work. But I really want a ListView to do the job :( i just don't get why it's so hard to do this in wpf kinda frustrating....</p>
  
  <pre><code>public partial class Page3 : Page
  {
  
      /*Variables passed through constructor*/
      private dbConnection connection;
      private List&lt;string&gt; selectedTables;
  
      public Page3(dbConnection connection, List&lt;string&gt; selectedTables)
      {
          InitializeComponent();
          this.connection = connection;
          this.selectedTables = selectedTables;
          init();
      }
  
      private void init()
      {
          dataGrid.ItemsSource = createDataTable().DefaultView;
      }
  
      private DataTable createDataTable()
      {
          DataTable dt = new DataTable();
          bool first = true;
  
          foreach (string selectedTableNames in selectedTables)
          {
              dt.Columns.Add(new DataColumn(selectedTableNames));
  
              List&lt;string&gt; tmpList = connection.GetColumnsList(selectedTableNames);
  
              for (int x = 0; x &lt; tmpList.Count; x++)
              {
                  if (first)
                  {
                      DataRow row = dt.NewRow();
                      row[selectedTableNames] = tmpList[x];
                      dt.Rows.Add(row);
                  }
                  else
                  {
                      DataRow row = dt.Rows[x];
  
                      if (row != null)
                      {
                          row[selectedTableNames] = tmpList[x];
                      }
                      else
                      {
                          row = dt.NewRow();
                          row[selectedTableNames] = tmpList[x];
                          dt.Rows.Add(row);
                      }
  
  
                  }
  
                  dt.AcceptChanges();
              }
  
              first = false;
  
              dt.AcceptChanges();
  
  
          }
  
          dt.AcceptChanges();
          return dt;
      }
  
      private void button_Click(object sender, RoutedEventArgs e)
      {
          Button b = (Button)sender;
  
          if (b.Equals(button_back))
          {
              NavigationService.GoBack();
          }
          else
          {
              //Go forward event here!
          }
      }
  }
  </code></pre>
  
  <p>And the XAML:</p>
  
  <pre><code>&lt;Page x:Class="HCCSVExport.Pages.Page3"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:local="clr-namespace:HCCSVExport.Pages"
    mc:Ignorable="d" 
    d:DesignHeight="300" d:DesignWidth="300"
    Title="Page3" MinWidth="519" MinHeight="321"&gt;
  
  &lt;Grid x:Name="grid" Background="White"&gt;
      &lt;Button x:Name="button_next" Content="volgende" Margin="0,0,10,10" VerticalAlignment="Bottom" Click="button_Click" HorizontalAlignment="Right" Width="86.493"/&gt;
      &lt;Button x:Name="button_back" Content="vorige" Margin="0,0,101,10" VerticalAlignment="Bottom" Click="button_Click" HorizontalAlignment="Right" Width="89" Uid="button_back"/&gt;
      &lt;Label x:Name="label" Content="Selecteer de kolommen uit de tabellen" Margin="10,10,10,0" VerticalAlignment="Top" FontSize="16" HorizontalContentAlignment="Center"/&gt;
      &lt;DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="10,46,0,0" VerticalAlignment="Top" Width="499" Height="240"/&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>