title: How to loop through each and every row, column and cells in a GridView and set its value ASP.NET
body: <p>I want to to loop through each and every row, column and cells. and then populate each column with a list.I have a list of employe object each employe has a id name and a city. I want to populate a gridview with the employe list. The first column should have the id of all the employes the second one should have the name of each employe and the third column should have the city of each employe.I am unable to get it to work any help would be very appreciated. </p>
  
  <p>Here is what my front end looks like.</p>
  
  <pre><code>       &lt;asp:GridView ID="GridView1" AllowPaging="true"  ShowFooter="true" PageSize="5" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowEditing="GridView1_RowEditing"
              runat="server"  CellPadding="3"  GridLines="None" CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt"
               OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowUpdating="GridView1_RowUpdating"&gt;
              &lt;Columns&gt;    
    &lt;asp:TemplateField HeaderText="name"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label  ID="lblname" CssClass="gridTextbox" Text="" runat="server" /&gt;
                  &lt;/ItemTemplate&gt;
                  &lt;EditItemTemplate&gt;
                      &lt;asp:TextBox CssClass="gridTextbox" ID="txtName" Text="" runat="server" /&gt;
                  &lt;/EditItemTemplate&gt;
                  &lt;FooterTemplate&gt;
                      &lt;asp:TextBox CssClass="gridTextbox" ID="txtNameFooter" runat="server" /&gt;
                  &lt;/FooterTemplate&gt;
              &lt;/asp:TemplateField&gt;
               &lt;asp:TemplateField HeaderText="city"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label  ID="lblcity" CssClass="gridTextbox" Text="" runat="server" /&gt;
                  &lt;/ItemTemplate&gt;
                  &lt;EditItemTemplate&gt;
                      &lt;asp:TextBox CssClass="gridTextbox" ID="txtCity" Text="" runat="server" /&gt;
                  &lt;/EditItemTemplate&gt;
                  &lt;FooterTemplate&gt;
                      &lt;asp:TextBox CssClass="gridTextbox" ID="txtCityFooter" runat="server" /&gt;
                  &lt;/FooterTemplate&gt;
              &lt;/asp:TemplateField&gt;          
                          &lt;ItemTemplate&gt;
                              &lt;asp:ImageButton ImageUrl="~/Images/edit.png" runat="server" CommandName="Edit" ToolTip="Edit" Width="20px" Height="20px"/&gt;
                              &lt;asp:ImageButton ImageUrl="~/Images/delete.png" runat="server" CommandName="Delete" ToolTip="Delete" Width="20px" Height="20px"/&gt;
                          &lt;/ItemTemplate&gt;
                          &lt;EditItemTemplate&gt;
                              &lt;asp:ImageButton ImageUrl="~/Images/save.png" runat="server" CommandName="Update" ToolTip="Update" Width="20px" Height="20px"/&gt;
                              &lt;asp:ImageButton ImageUrl="~/Images/cancel.png" runat="server" CommandName="Cancel" ToolTip="Cancel" Width="20px" Height="20px"/&gt;
                          &lt;/EditItemTemplate&gt;
                          &lt;FooterTemplate&gt;
                              &lt;asp:ImageButton ImageUrl="~/Images/addnew.png" runat="server" CommandName="AddNew" ToolTip="Add New" Width="20px" Height="20px"/&gt;
                          &lt;/FooterTemplate&gt;
           &lt;/asp:TemplateField&gt;
                  &lt;/Columns&gt;
         &lt;/asp:GridView&gt; 
  </code></pre>
  
  <p>Here is what my back end looks like.</p>
  
  <pre><code>  protected List&lt;Emp&gt; GetEmpList()
          {
              List&lt;Emp&gt; lEmp = new List&lt;Emp&gt;();
              Emp oemp = new Emp(1234, "Upendra", "Noida");
              lEmp.Add(oemp);
              oemp = new Emp(1234, "Upendra", "Noida");
              lEmp.Add(oemp);
              oemp = new Emp(1374, "Vishal", "Noida");
              lEmp.Add(oemp);
              oemp = new Emp(1934, "Rahul", "Noida");
              lEmp.Add(oemp);
  
              return lEmp;
          }
  
          protected void BindGridList()
          {    
              for (int i = 0; i &lt; GridView1.Columns.Count - 1; i++)
              {
                  foreach (GridViewRow row in GridView1.Rows)
                  {
                      row.Cells[i].Text = GetEmpList()[i].ID.ToString();
                  }    
              }
          }
  
  
      }
      public class Emp
      {
          public int ID { get; set; }
          public string Name { get; set; }
          public string City { get; set; }
          public Emp(int id, string name, string city)
          {
              this.ID = id;
              this.Name = name;
              this.City = city;
          }
    }
  </code></pre>