Alexa Adrian
Alexa Adrian

Reputation: 1858

How to add to each cell in a asp GridView a div wrapper?

Can you tell me how to add to each cell a div inside each td generated?

<asp:GridView ID="XDataGridView"
        CssClass="XDataGridView"
        ClientIDMode="Static"
        AllowPaging="True"
        AllowSorting="True"
        OnSorting="XDataGridView_Sorting"
        OnPageIndexChanging="XDataGridView_PageIndexChanging"
        Font-Size="Smaller"
        runat="server"
        EnablePersistedSelection="True"
        OnRowDataBound="XDataGridView_RowDataBound"
        OnRowCreated="XDataGridView_RowCreated"
        SelectedRowStyle-BackColor="#FF7900"
        meta:resourcekey="XDataGridViewResource1">
        <HeaderStyle CssClass="XDataGridViewHeader" BackColor="DimGray"
            BorderColor="#FF7900" HorizontalAlign="Center" VerticalAlign="Middle" Font-Size="10" Height="34" />
        <PagerSettings Visible="false" />
        <RowStyle BorderColor="#FF7900" Height="34" Font-Size="10" />
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <div id="insideDiv" runat="server"></div>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

I need that each time the grid will generate its table in html each td content will be wrappped in a div element. At first sight should be something easy though I don't know how to do. I've tried with ItemTemplate but doesn't seem to make any difference.

Upvotes: 0

Views: 697

Answers (1)

VDWWD
VDWWD

Reputation: 35514

You can do this in the ItemTemplate.

<ItemTemplate>
    <div><%# Eval("columnA") %></div>
</ItemTemplate>

The div element does not need the runat="server" to function.

UPDATE

If you use AutoGenerated columns, you can use the RowDataBound event to add the <div>.

protected void XDataGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //check if the row is a datarow
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //loop all the cells in the row
        for (int i = 0; i < e.Row.Cells.Count; i++)
        {
            e.Row.Cells[i].Text = "<div>" + e.Row.Cells[i].Text + "</div>";
        }
    }
}

Upvotes: 1

Related Questions