views:

247

answers:

1

Hello,

I have a DataRepeater with a GridView inside to display some tables from a stored procedure.

Here is my repeater code:

<asp:Repeater ID="rptResults" runat="server" OnItemDataBound="rptResults_ItemDataBound">
    <ItemTemplate>
        <div style="width: 1100px; overflow: scroll;">
            <asp:GridView ID="gvResults" runat="server" BackColor="LightGoldenrodYellow" BorderColor="Tan"
                BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None">
                <FooterStyle BackColor="Tan" Wrap="false" />
                <RowStyle Wrap="false" />
                <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center"
                    Wrap="false" />
                <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" Wrap="false" />
                <HeaderStyle BackColor="Tan" Font-Bold="True" Wrap="false" />
                <AlternatingRowStyle BackColor="PaleGoldenrod" Wrap="false" />
            </asp:GridView>
            <asp:Label runat="server" ID="lblNoRecords" Visible="false"></asp:Label>
        </div>
        <br />
    </ItemTemplate>
</asp:Repeater>

I have as my repeater binding:

    rptResults.DataSource = results.Tables;
    rptResults.DataBind();

I have the following for my GridView binding for each table:

protected void rptResults_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var o = e.Item.DataItem as DataTable;
                if (o.Rows.Count == 0)
                {
                    var lblNoRecords = (Label) e.Item.FindControl("lblNoRecords");
                    lblNoRecords.Text = "No Records";
                    lblNoRecords.Visible = true;
                }
                else
                {
                    var gv = (GridView)e.Item.FindControl("gvResults");
                    gv.DataSource = o;

                    gv.DataBind();    
                }

            }
        }

The data that comes back could change, since it is a stored procedure, and it will always return an n number of tables.

I would like to try to get my columns to auto size based on the data that is being returned. Right now it squishes most of the data, including date/time data.

I can't seem to figure out how to do this.

Thoughts?

+1  A: 

Sorry if this is irrelevant, but I'm reading between the lines of your question and wondering if you are just trying to get certain columns (such as your date/time columns) not to wrap their contents, thus deferring the wrap to other columns (such as text columns)? The browser will generally try to layout the HTML table optimally. If you want certain columns not to wrap, you can use the CSS white-space: nowrap property.

In a GridView using ItemTemplate, there is a Wrap="False" property:

<ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Data") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Wrap="False" />

If you want to use the auto-generated columns, you will have to handle the appropriate events on each generated GridView and then set the attributes in code-behind. Something like:

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    e.Row.Cells[2].Attributes.Add("style", "white-space: nowrap;");
}

Obviously you would first need to determine if the bound column data is of the type for which you want to forbid wrapping.

The above is the worst-case scenario; you may get away with handling events that don't fire as much (per-column perhaps), but I'm not sure if that will work in your situation.

If you want the entire table not to wrap, you may be able to get away with just setting the CSS property on your page for all table.tr.td elements.

Dave
your second section worked like a charm! thanks!!
Jason Heine