NiteshG86
NiteshG86

Reputation: 85

How Can we Render Html text in DataGridView?

I have html text data and i want to bind this data in my gridview cell.

but my datagridview is not formet html text.

exp:

string htmltext = "<p>hi some text here</p>";
dgrow.rows[0].cell[1].value=htmltext;

In this case cell value of my gridview is contain Html tag also. so how can I format Html Text in my grid view?

Upvotes: 0

Views: 4136

Answers (2)

Ocean Airdrop
Ocean Airdrop

Reputation: 2921

I also recently had the requirement to render HTML text in a WinForms DataGridView control.

Like you I couldnt find anything out there that fitted my needs so created my own custom DataGridViewHTMLCell class.

You can find the code and sample project here: https://github.com/OceanAirdrop/DataGridViewHTMLCell

Upvotes: 1

hutchonoid
hutchonoid

Reputation: 33305

In the mark-up (please note the Mode is PassThrough):

<asp:GridView ID="GridView1" runat="server">
  <Columns>
   <asp:TemplateField>
     <ItemTemplate>
       <asp:Literal ID="literal1" Mode="PassThrough" runat="server"></asp:Literal>
     </ItemTemplate>
  </asp:TemplateField>
 </Columns>

Use the RowDataBound event to find the literal and set the value:

void GridView1GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{ 
  if(e.Row.RowType == DataControlRowType.DataRow)
  {
    //find the literal
    var literal1 = e.Item.FindControl("literal1") as Literal;
    if (literal1 != null)
    {
      literal1.Text = "<p>hi some text here</p>";
    }
   }
  }

Please forgive any errors, I have not tested the above.

Upvotes: 0

Related Questions