Dynamic GridView with all Formatting and template fields
<p>I want to add gridview dynamically with all formatting and with template fields ..</p>

<p>i m trying it by below way.</p>

<pre><code>public StringBuilder getTextForGridView()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine(" &lt;asp:GridView ID=\"GridView1\" runat=\"server\" AutoGenerateColumns=\"False\" ");
sb.AppendLine(" CellPadding=\"4\" ForeColor=\"#333333\" GridLines=\"None\" ShowHeader=\"true\"&gt;");

sb.AppendLine(" &lt;Columns&gt;");

sb.AppendLine(" &lt;asp:TemplateField&gt;");
sb.AppendLine(" &lt;HeaderTemplate&gt;");

sb.AppendLine(" &lt;asp:Label ID=\"lbl1\" runat=\"server\" Text='&lt;%#Eval(\"Id\")%&gt;'&gt;&lt;/asp:Label&gt;");
sb.AppendLine(" &lt;/HeaderTemplate&gt;");
sb.AppendLine(" &lt;ItemTemplate&gt;");
sb.AppendLine(" &lt;asp:Label ID=\"lbl1\" runat=\"server\" Text='&lt;%#Eval(\"Id\")%&gt;'&gt;&lt;/asp:Label&gt;");

sb.AppendLine(" &lt;/ItemTemplate&gt;");

sb.AppendLine(" &lt;/asp:TemplateField&gt;");

sb.AppendLine(" &lt;asp:TemplateField&gt;");
sb.AppendLine(" &lt;HeaderTemplate&gt;");

sb.AppendLine(" &lt;asp:Label ID=\"lbl1\" runat=\"server\" Text='&lt;%#Eval(\"Name\")%&gt;'&gt;&lt;/asp:Label&gt;");
sb.AppendLine(" &lt;/HeaderTemplate&gt;");
sb.AppendLine(" &lt;ItemTemplate&gt;");
sb.AppendLine(" &lt;asp:Label ID=\"lbl1\" runat=\"server\" Text='&lt;%#Eval(\"Name\")%&gt;'&gt;&lt;/asp:Label&gt;");

sb.AppendLine(" &lt;/ItemTemplate&gt;");

sb.AppendLine(" &lt;/asp:TemplateField&gt;");
sb.AppendLine(" &lt;/Columns&gt;");

sb.AppendLine(" &lt;/asp:GridView&gt;");

return sb;
}
</code></pre>

<p>then i m assigning below text to literal as and adding it to placeholder</p>

<pre><code>Literal li = new Literal();
li.ID = "lit";
li.Text = getTextForGridView().ToString();

PlaceHolder1.Controls.Add(li);
</code></pre>

<p>but the problem is i m not able to assign datasource to gridview as i m not able to get object of gridview..
Please get me out of here.
Thanks.</p>
