【问题标题】:GridView inside Repeater Column Width中继器列宽内的 GridView
【发布时间】:2012-04-12 22:41:17
【问题描述】:

我有一个内部带有 GridView 的 DataRepeater,用于显示存储过程中的一些表。

这是我的中继器代码:

<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>

我有我的转发器绑定:

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

我对每个表的 GridView 绑定都有以下内容:

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();    
                }

            }
        }

返回的数据可能会改变,因为它是一个存储过程,它总是会返回 n 个表。

我想尝试根据返回的数据使我的列自动调整大小。现在它压缩了大部分数据,包括日期/时间数据。

我似乎不知道该怎么做。

想法?

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    对不起,如果这无关紧要,但我正在阅读您的问题,想知道您是否只是试图让某些列(例如您的日期/时间列)不包装它们的内容,从而推迟包装到其他列(例如文本列)?浏览器通常会尝试以最佳方式布局 HTML 表格。如果您希望某些列不换行,可以使用 CSS white-space: nowrap 属性。

    在使用 ItemTemplate 的 GridView 中,有一个 Wrap="False" 属性:

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

    如果您想使用自动生成的列,您必须在每个生成的 GridView 上处理适当的事件,然后在代码隐藏中设置属性。比如:

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

    显然,您首先需要确定绑定的列数据是否属于您要禁止包装的类型。

    以上是最坏的情况;您可能会处理不那么触发的事件(可能是每列),但我不确定这是否适用于您的情况。

    如果您希望整个表格不换行,您可能只需在页面上为所有table.tr.td 元素设置CSS 属性即可。

    【讨论】:

      【解决方案2】:

      这对我有用

      <asp:GridView ID="SomeGridView"  CssClass="gridtext" > 
      
      .gridtext  
      {
          white-space: nowrap;
      }
      
      .gridtext table
      {
          white-space: nowrap;
      }
      .gridtext tr
      {
          white-space: nowrap;
      }
      .gridtext td
      {
          white-space: nowrap;
      }
      
      .gridtext th
      {
          white-space: nowrap;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多