title: Fill GridView with TextBoxes and Dropdownlist from DataTable
body: <p>I have GridView:</p>
  
  <pre><code>  &lt;asp:GridView ID="MyGridView" runat="server" ShowFooter="true"
                                       AutoGenerateColumns="False" Visible="True"&gt;
  &lt;Columns&gt;
  &lt;asp:BoundField DataField="id" ItemStyle-HorizontalAlign="center"/&gt;
  &lt;asp:BoundField DataField="fullName"  /&gt;
  &lt;asp:TemplateField HeaderText="situation&gt;"&gt;
  &lt;ItemTemplate&gt;
    &lt;asp:DropDownList ID="dl_situation" runat="server" AppendDataBoundItems="true"&gt;
     &lt;/asp:DropDownList&gt;
  &lt;/ItemTemplate&gt;
  &lt;/asp:TemplateField&gt;
  
  &lt;asp:TemplateField HeaderText="procesVerbal"&gt;
   &lt;ItemTemplate&gt;
           &lt;asp:TextBox ID="tbNr" runat="server"&gt;&lt;/asp:TextBox&gt;
  &lt;/ItemTemplate&gt;
  &lt;/asp:TemplateField&gt;  
  
  &lt;asp:TemplateField HeaderText="Data"&gt;
  &lt;ItemTemplate&gt;
                 &lt;asp:TextBox ID="tbDate" runat="server"&gt;&lt;/asp:TextBox&gt;
   &lt;/ItemTemplate&gt;
  &lt;/asp:TemplateField&gt;     
  &lt;/Columns&gt;
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>Now I want to fill this Grid with data from database, I have one method that returns datatable with all my data</p>
  
  <p>when I fill mygrid: </p>
  
  <pre><code> MyGridView.DataSource = dataTable;
   MyGridView.Databind();
  </code></pre>
  
  <p>in this case all TextBoxes and DropDownList are null.</p>
  
  <p>In the code below I'm trying to create a function that receives DataTable from database and populates gridview with data. How do I make a foreach statement for dataTable.Rows that will assign text values to TextBox elements and selectedIndex value to DropDownList?</p>
  
  <pre><code>protected bool FillGridWithData(DataTable dataTable)
         {bool result;
         try
         {
             foreach (GridViewRow row in MyGridView1.Rows)
             {
                 if (row.RowType != DataControlRowType.DataRow) break;
                 var ddl = (DropDownList)row.FindControl("dl_situation");
                 if (ddl != null)
                 {
                   ddl.DataSource = PublicStatic.Situation;
                   ddl.DataTextField = PublicStatic.Name;
                   ddl.DataValueField = PublicStatic.Code;
                   ddl.DataBind();
                   ddl.Items.Insert(0, new ListItem(String.Empty, String.Empty));
                   ddl.SelectedIndex = //data from datatable;
                 }
                 var tb1 = (TextBox)row.FindControl("tbNr");
                 if (tb1 != null)
                     tb1.Text =//data from datatable;
  
                 var tb2 = (TextBox)row.FindControl("tbDate");
                 if (tb2 != null)
                     tb2.Text = //data from datatable;
             }
             result = true;
         }
         catch (Exception exception)
         {
             result = false;
         }
         return result;
     }
  </code></pre>