Before getting to the code I will give a small introduction how to work with gridview with HTMl Tags.
- Add a gridview controls to the ASPX Page.
- Add AutoGenerateColumns="false" in agridview controls
- Add a columns inside a gridview.
- Add a Bound field inside a columns.
- Add a HtmlEncode="false" to the bound column where you are using a HTML Tags.
- Provide a datasource to a GridView.
- Bind a GridView
Description:
Below is a sample code.
In ASPX Page
Expand|Select|Wrap|Line Numbers
- <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
- <Columns>
- <asp:BoundField DataField="Column1" HeaderText="Column1" />
- <asp:BoundField HtmlEncode="false" DataField="Column2" HeaderText="Column2" />
- </Columns>
- </asp:GridView>
Expand|Select|Wrap|Line Numbers
- protected void Page_Load(object sender, EventArgs e)
- {
- DataTable dtTable = CreateDataTable();
- GridView1.DataSource = dtTable;
- GridView1.DataBind();
- }
- DataTable CreateDataTable()
- {
- DataTable dtNew = new DataTable();
- dtNew.Columns.Add("Column1");
- dtNew.Columns.Add("Column2");
- DataRow dr = dtNew.NewRow();
- dr["Column1"] = "Test";
- dr["Column2"] = "<p>My Address Line1</p><p>My Address Line2</p>";
- dtNew.Rows.Add(dr);
- return dtNew;
- }
- Summary