views:

367

answers:

3

Using aspnet 3.5, c# - Is there a way to insert Html into a gridview row?

+3  A: 

Simply modify the Text property of a cell.

Steven
Wow. Cant believe how simple that was. Was going crazy with this.
Lill Lansey
A: 

I haven't tested this, but you should be able to add a Label control to the GridView cell. Then write your HTML to the Label's Text property. The Label should render the HTML.

Eclipsed4utoo
Had tried this: it just prints out the html as text, is : <b>Make this bold</> with the view source using the asci chars for < and > and /
Lill Lansey
This is not a good approach. And also a Label wraps the text property values between a <span> tag.
Raúl Roa
@Lansey , I tried it right after I posted and it worked for me. I used "<b>hey</b> hey" and the first hey was bolded and the second was not.
Eclipsed4utoo
+3  A: 

Yes. Use the TemplateField, and then type your html directly into the markup. If the html is suppose to be dynamically created I would use a Literal instead of a Label.

<asp:GridView id="GridView1" runat="server">
    <Columns>
        <asp:TemplateField headertext="Column1">
            <ItemTemplate>
                <br />
                <h1>
                    <%# Eval ("DataColumnName") %>
                </h1>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField headertext="Column2">
            <ItemTemplate>
                <asp:Literal id="Literal1" runat="server" text='<%# Eval ("DataColumnName2") %>'></asp:Literal>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
Chris
Thanks! This worked. So did Steven's answer. Both are easy solutions.
Lill Lansey
Am going with this solution. Not hardcoding column position into my code and also consistent with the other ways I am dynamically populating my GridView: Literal theLit = (Literal)r.FindControl("Literal1"); theLit.Text=@"<b>Make this bold</b>";
Lill Lansey