title: Gridview inside Datalist ASP.NET
body: <p>I am trying to place an asp gridview inside an asp datalist. For each item in the datalist I would like certain values from another database table to be returned.</p>
  
  <p>I have written the following code, but the gridview returns not data (I can confirm there is records in the database and the stored procedure is correct). The asp datalist is working fine.</p>
  
  <p>What am I doing wrong that is not populating the gridview?</p>
  
  <pre><code>&lt;asp:DataList runat="server" id="listResponses" DataKeyField="QuestionID" CssClass="confirm" OnItemDataBound="listResponses_ItemDataBound"&gt;
      &lt;ItemTemplate&gt;
      &lt;h3 class="confirm new"&gt;Question &lt;asp:Label ID="lblaOrder" runat="server" Text='&lt;%# Container.ItemIndex  + 1 %&gt;'&gt;&lt;/asp:Label&gt;&lt;/h3&gt;
      &lt;div class="confirm_question"&gt;
          &lt;asp:Label ID="lblOrder" runat="server" Text='&lt;%# Container.ItemIndex  + 1 %&gt;'&gt;&lt;/asp:Label&gt;
          &lt;p class="confirm"&gt;&lt;%# DataBinder.Eval(Container.DataItem, "QuestionText") %&gt;&lt;/p&gt;
      &lt;/div&gt; &lt;!-- end confirm_question --&gt;
      &lt;asp:GridView runat="server" ID="gridResponses" DataKeyNames="QuestionID""&gt;
          &lt;Columns&gt;
              &lt;asp:BoundField DataField="AnswerTitle" HeaderText="ID" HeaderStyle-Width="80px" ItemStyle-CssClass="bo"&gt;&lt;/asp:BoundField&gt;
              &lt;asp:BoundField DataField="Responses" HeaderText="Response Count" HeaderStyle-Width="150px" /&gt;
           &lt;/Columns&gt;
        &lt;/asp:GridView&gt;   
    &lt;/ItemTemplate&gt; 
  &lt;/asp:DataList&gt;
  </code></pre>
  
  <p>And here is the code behind.</p>
  
  <pre><code>public partial class questionnaire_responses : System.Web.UI.Page
  {
      OsqarSQL GetData;
      DataTable DT;
      private string _productConnectionString;
      private SqlConnection _productConn;      
  
      protected void Page_Load(object sender, EventArgs e) 
      { 
          string questionnaireId = Session["qID"].ToString();
          int qid = Convert.ToInt32(questionnaireId);
          GetData = new OsqarSQL();
          string name = GetData.GetQuestionnaireName(qid);
          lblQuestionnaireName.Text = name;
          if (!IsPostBack) 
          { 
              DT = GetData.GetQuestionNameDataList(qid);
              listResponses.DataSource = DT;
              listResponses.DataBind();
          } 
      }
  
      private void BindGrid(GridView GridView, int questionId) 
      {
          DataTable dt = new DataTable(); 
          GetData.GetAnswerTitle(questionId);
          GridView.DataSource = dt; 
          GridView.DataBind();
      }
  
      protected void listResponses_ItemDataBound(object sender, DataListItemEventArgs e) 
      { 
          GridView gridResponses=(GridView)e.Item.FindControl("gridResponses"); 
          BindGrid(gridResponses, (int)listResponses.DataKeys[e.Item.ItemIndex]); 
      }
  
  }
  
  //Method from the data access class
  public DataTable GetAnswerTitle(int QuestionId)
  {
      string returnValue = string.Empty;
      SqlCommand myCommand = new SqlCommand("GetAnswer", _productConn);
      myCommand.CommandType = CommandType.StoredProcedure;
      ,yCommand.Parameters.Add(new SqlParameter("@QUESTION_ID", SqlDbType.Int));
      myCommand.Parameters[0].Value = QuestionId;
      return createDataTable(getData(myCommand));
  }
  </code></pre>