10

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

1

3 Answers 3

13

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! This worked. So did Steven's answer. Both are easy solutions.
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>";
3

Simply modify the Text property of a cell.

Comments

1

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.

3 Comments

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 /
This is not a good approach. And also a Label wraps the text property values between a <span> tag.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.