title: How retrieve SQL query result to show in grid view header? ASP.NET C#
body: <p>I have table in SQL Server - here's a screen shot is </p>
  
  <p><a href="http://i.stack.imgur.com/bypjG.png" rel="nofollow">LINK</a>.</p>
  
  <p>I have <code>SqlCommand</code> which retrieves columns from the table, and I want to show these column in a gridview. Also, I want to insert a textbox in every gridview header and an <code>INSERT</code> button in the gridview row.</p>
  
  <p>When the user inserts values and presses <code>INSERT</code> then new row is generated and same user again insert same like we insert data in table.I want to generate <code>textbox</code> dynamically according to gridview columns. Columns may be 1, 2 or 10. It depends on the user table.</p>
  
  <p>ASPX markup:</p>
  
  <pre><code>&lt;asp:Content ID="Content1" ContentPlaceHolderID="title" Runat="Server"&gt;
      &lt;title&gt;Insert Data&lt;/title&gt;
  &lt;/asp:Content&gt;
  &lt;asp:Content ID="Content2" ContentPlaceHolderID="body" Runat="Server"&gt;
      &lt;form id="form" runat="server"&gt;
          &lt;asp:GridView ID="GridView1" runat="server"&gt;
          &lt;/asp:GridView&gt;
  
          &lt;asp:Button ID="Button1" runat="server" OnClick="load" Text="Button" /&gt;
      &lt;/form&gt;
  &lt;/asp:Content&gt;
  </code></pre>
  
  <p>Code behind:</p>
  
  <pre><code>public void load(object sender, EventArgs e)
  {
      //string tbl_name = Session["value"].ToString();
      //string tbl_name = "new";
      string query = "USE db_compiler SELECT Column_Name FROM tbl_field WHERE Table_Name='pak'";
  
      SqlCommand cmd = new SqlCommand(query, cnn);
  
      DataTable dt = new DataTable();
      SqlDataAdapter adp = new SqlDataAdapter(cmd);
  
      DataTable table = new DataTable();
  
      cnn.Open();
      cmd.ExecuteNonQuery();
      adp.Fill(dt);
  
      for (int i = 0; i &lt;= dt.Rows.Count; i++)
      {
           table.Columns.Add(Convert.ToString(i));
      }
  
      DataRow dr;
  
      //get all the columns and make it as rows
      for (int j = 0; j &lt; dt.Columns.Count; j++)
      {
          dr = table.NewRow();
          dr[0] = dt.Columns[j].ToString();
  
          for (int k = 1; k &lt;= dt.Rows.Count; k++)
              dr[k] = dt.Rows[k - 1][j];
  
          table.Rows.Add(dr);
      }
  
      GridView1.DataSource = table;
      GridView1.DataBind();
  
      cnn.Close();
  }
  </code></pre>
  
  <p>I want <code>name</code>,<code>id</code> ,<code>city</code> shown in <code>GRIDVIEW</code> header and in every <code>GRIDVIEW</code> column dynamically generate <code>textbox</code> according to <code>header</code> and at the end <code>INSERT</code> button.</p>
  
  <p>Thanks</p>