title: Listbox within a DataGrid
body: <p>Could someone please point me in the right direction regarding the above?</p>
  
  <p>I have a datagrid returning multiple rows via sqlite query. Within each row I need a listbox which should return multiple items associated with that row. I’m returning the reference id from the parent table in the datagrid as a column but can’t seem to find a solution to return the child records in the listbox.</p>
  
  <p>I’m thinking I need to do this via a “DataGridTemplateColumn” or something.</p>
  
  <p>Question 1: How would this be structured in the xaml
  Question 2: How would I iterate through sql to present datagrid rows with child listbox items in code behind or any other method</p>
  
  <p>Graphical Representation of what I need
  <img src="https://austransmittals.apachecorp.com/Transmittals/Test/datagrid.png" /></p>
  
  <p><b>NOTE: Each ListBox needs to be editable as well</b></p>
  
  <pre><code>public void getItems()
      {
          try
          {
              string thisPermitID = "1";
  
              DataTable potentialHazards;
              String query = "select JSAPH.JobSafetyAnalysisStepPotentialHazardID \"JobSafetyAnalysisStepPotentialHazardID\""
                  + ", JSA.RefPermitID \"RefPermitID\""
                  + ", JSAPH.RefJobSafetyAnalysisStepID \"RefJobSafetyAnalysisStepID\""
                  + ", JSAS.StepDescription \"StepDescription\""
                  + ", JSAPH.PotentialHazard \"PotentialHazard\""
                  + " from JobSafetyAnalysis JSA"
                  + " inner join JobSafetyAnalysisStep JSAS on JSAS.RefJobSafetyAnalysisID = JSA.JobSafetyAnalysisID"
                  + " inner join JobSafetyAnalysisStepPotentialHazard JSAPH on JSAPH.RefJobSafetyAnalysisStepID = JSAS.JobSafetyAnalysisStepID"
                  + " where RefPermitID = '" + thisPermitID + "'";
  
              potentialHazards = db.GetDataTable(query);
  
              List&lt;Item&gt; PotentialHazardSelectedList = new List&lt;Item&gt;();
  
              foreach (DataRow r in potentialHazards.Rows)
              {
                  if (Items != null)
                  {
                      Items.Add(new Item()
                      {
                          Name = r["StepDescription"].ToString(),
                          ItemCollection = new ObservableCollection&lt;string&gt;() { r["PotentialHazard"].ToString() }
                      });
                  }
  
              }
  
              this.DataContext = Items;
  
          }
          catch (Exception fail)
          {
              String error = "The following error has occurred:\n\n";
              error += fail.Message.ToString() + "\n\n";
              MessageBox.Show(error);
          }
      }
  </code></pre>