title: How do I fetch a set of rows from data table
body: <p>I have a dataset that has two datatables.</p>
  
  <p>In the first datatable I have EmpNo,EmpName and EmpAddress</p>
  
  <p>In the second datatable I have Empno,EmpJoindate, EmpSalary.</p>
  
  <p>I want a result where I should show EmpName as the label and his/her details in the gridview</p>
  
  <p>I populate a datalist with the first table, and have EmpNo as the datakeys.</p>
  
  <p>Then I populate the gridview inside the datatable which has EmpNo,EmpJoinDate and EmpAddress.</p>
  
  <p>My code is some what as below</p>
  
  <pre><code>       Datalist1.DataSource = dt;
          Datalist1.DataBind();
  
          for (int i = 0; i &lt; Datalist1.Items.Count; i++)
          {
              int EmpNo = Convert.ToInt32(Datalist1.DataKeys[i]);
              GridView gv = (GridView)Datalist1.FindControl("gv");
              gv.DataSource = dt2;
              gv.DataBind();
          }
  </code></pre>
  
  <p>Now I have a problem, I have to bind the Details of the corresponding Employee to the gridview. Whereas the above code will display all the details of all employees in the gridview.</p>
  
  <p>If we use IEnumerable we give a condition where(a=>a.eno=EmpNo), and bind that list to the gridview.</p>
  
  <p>How do I do this in datatable.</p>
  
  <p>kindly do not give me suggestions of altering the stored procedure which results the values in two tables, because that cannot be altered.
  I have to find a solution within the existing objects I have.</p>
  
  <p>Regards
  Hema</p>
  
  <p>Hi,
  I modified the program as below
  Igor, instead of datarelation,(my TL did not approve of it)  I used IEnumerable</p>
  
  <p>DataTable dt = new DataTable();
          dt.Columns.Add("EmpNo");
          for (int i = 65; i &lt; 70; i++)
          {
              DataRow dr = dt.NewRow();
              dr["EmpNo"] = i.ToString();
              dt.Rows.Add(dr);
          }</p>
  
  <pre><code>    DataTable dt2 = new DataTable();
      dt2.Columns.Add("EmpNo");
      dt2.Columns.Add("EmpName");
      for (int i = 65; i &lt; 70; i++)
      {
          DataRow dr = dt2.NewRow();
          dr["EmpNo"] = i.ToString();
  
          dr["EmpName"] = Convert.ToChar(i);
          dt2.Rows.Add(dr);
      }
  
  
      Datalist1.DataSource = dt;
      Datalist1.DataBind();
      IEnumerable&lt;DataRow&gt; sequence = dt2.AsEnumerable();
  
      for (int i = 0; i &lt; Datalist1.Items.Count; i++)
      {
          string EmpNo = Datalist1.DataKeys[i].ToString();
          string strExpr = "EmpNo =" + EmpNo.ToString();
          GridView Gridview1 = (GridView)Datalist1.Items[i].FindControl("Gridview1");
          Gridview1.DataSource = sequence.Where(t =&gt; t.Field&lt;string&gt;("EmpNo") == EmpNo);
          Gridview1.DataBind();
      }
  </code></pre>
  
  <p>My Designer.aspx is as below</p>
  
  <pre><code> &lt;asp:DataList ID="Datalist1" runat="server" DataKeyField="EmpNo"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:GridView ID="Gridview1" runat="server"&gt;
                  &lt;/asp:GridView&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:DataList&gt;
  </code></pre>
  
  <p>On execution I do not observe any results, and I see errors as</p>
  
  <p>RowError HasErrors on the screen</p>
  
  <p>I included in the gridview the bound field control
  as</p>
  
  <pre><code>&lt;asp:GridView ID="Gridview1" AutoGenerateColumns="true" runat="server"&gt;
                  &lt;Columns&gt;
                  &lt;asp:BoundField DataField="EmpNo" /&gt;
                  &lt;/Columns&gt;
                  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>And on execution, it throws an error stating
  A field or property with the name 'EmpNo' was not found on the selected data source.</p>
  
  <p>I checked the "sequence" using Debug mode, and it had values in the array,
   which I have highlighted,</p>
  
  <p>So where am I going wrong</p>
  
  <ul>
  <li>Results View    Expanding the Results View will enumerate the IEnumerable   </li>
  <li>[0] {System.Data.DataRow}   System.Data.DataRow
      HasErrors   false   bool</li>
  <li>ItemArray   {object[2]} object[]
      <strong>[0]   "65"    object {string}
      [1] "A" object {string}</strong>
      RowError    ""  string
      RowState    Added   System.Data.DataRowState</li>
  <li>Table   {}  System.Data.DataTable</li>
  <li>Static members      </li>
  <li>Non-Public members      </li>
  </ul>