title: How to retrieve values from database and row in gridview based on rows in table?
body: <p>I want to retrieve values in gridview based on the rows in database</p>
  
  <p>I tried it's working but values are not there in controls</p>
  
  <p>I have 2 rows in table </p>
  
  <p><strong>Edit:</strong></p>
  
  <pre><code>&lt;asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="true"&gt;
      &lt;Columns&gt;
          &lt;asp:TemplateField HeaderStyle-BackColor="DarkCyan" HeaderText="Activities"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:DropDownList ID="dropActivities" Width="150px" runat="server"&gt;&lt;/asp:DropDownList&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:TemplateField HeaderStyle-BackColor="OliveDrab" HeaderText="Monday"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:TextBox ID="TextBox1" runat="server"&gt;&lt;/asp:TextBox&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:TemplateField HeaderStyle-BackColor="OliveDrab" HeaderText="Tuesday"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:TextBox ID="TextBox2" runat="server"&gt;&lt;/asp:TextBox&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:TemplateField HeaderStyle-BackColor="OliveDrab" HeaderText="Wednesday"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:TextBox ID="TextBox3" runat="server"&gt;&lt;/asp:TextBox&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:TemplateField HeaderStyle-BackColor="OliveDrab" HeaderText="Thursday"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:TextBox ID="TextBox4" runat="server"&gt;&lt;/asp:TextBox&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:TemplateField HeaderStyle-BackColor="OliveDrab" HeaderText="Friday"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:TextBox ID="TextBox5" runat="server"&gt;&lt;/asp:TextBox&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:TemplateField HeaderStyle-BackColor="OliveDrab" HeaderText="Saturday"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:TextBox ID="TextBox6" runat="server"&gt;&lt;/asp:TextBox&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:TemplateField HeaderStyle-BackColor="OliveDrab" HeaderText="Sunday"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:TextBox ID="TextBox7" runat="server"&gt;&lt;/asp:TextBox&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
      &lt;/Columns&gt;
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      if(!IsPostback)
      {
        bind();
      }
  }
  
  private void bind()
  {
      int rowIndex = 0;
      DataTable dt = new DataTable();
      DataRow drow = null;
      dt.Columns.Add(new DataColumn("Column0", typeof(string)));
      dt.Columns.Add(new DataColumn("Column1", typeof(string)));
      dt.Columns.Add(new DataColumn("Column2", typeof(string)));
      dt.Columns.Add(new DataColumn("Column3", typeof(string)));
      dt.Columns.Add(new DataColumn("Column4", typeof(string)));
      dt.Columns.Add(new DataColumn("Column5", typeof(string)));
      dt.Columns.Add(new DataColumn("Column6", typeof(string)));
      dt.Columns.Add(new DataColumn("Column7", typeof(string)));
  
          OleDbCommand cmd = new OleDbCommand("select activities,monday,tuesday,wednesday,thursday,friday,saturday,sunday from activity_values where week = '" + weekNumNow + "' and emp_id = (select emp_id from emp_master where username = '" + username + "')", DbConnection);
          DbConnection.Open();
          OleDbDataReader dr1 = cmd.ExecuteReader();
  
          while (dr1.Read())
          {
              drow = dt.NewRow();
              //Here I can get the values
              drow["Column0"] = dr1[0].ToString();
              drow["Column1"] = dr1[1].ToString();
              drow["Column2"] = dr1[2].ToString();
              drow["Column3"] = dr1[3].ToString();
              drow["Column4"] = dr1[4].ToString();
              drow["Column5"] = dr1[5].ToString();
              drow["Column6"] = dr1[6].ToString();
              drow["Column7"] = dr1[7].ToString();
              dt.Rows.Add(drow);
              dt.AcceptChanges();
  
              rowIndex++;
          }
          ViewState["CurrentTable"] = dt;
          Gridview1.DataSource = dt;
          Gridview1.DataBind();
      }
  </code></pre>
  
  <p>In the gridview rows are added but showing empty values
  <img src="https://i.stack.imgur.com/GVcxI.png" alt=""></p>
  
  <p>Any ideas? Thanks in advance</p>