【问题标题】:Adjusting width of column to fit text onto page in GridView AutoGenerateColumn调整列的宽度以使文本适合 GridView AutoGenerateColumns 中的页面
【发布时间】:2018-08-13 19:40:39
【问题描述】:

我在尝试调整每行的列或单元格以使文本不会像当前那样水平或垂直跨越时遇到问题:

正如您在照片中看到的,"Summary" 列下的文本太长,导致页面太宽,因此用户必须滚动才能看到其他列。我尝试了多种方法来调整单元格的宽度,以便限制文本,但我似乎无法让它工作。我在我的代码中尝试了以下内容:

e.Row.Cells[3].Wrap = false;  //Works but makes it so text spans horizontally as opposed to vertically
e.Row.Cells[3].Attributes.Add("style", "word-wrap:break-word;"); //Does not work
e.Row.Cells[3].Width = new Unit("50px"); //Does not work

我唯一能做的就是将 wrap 属性调整为 false,但这样做会使文本跨度过宽。如果我将其保留为真,我会在"Quick_Sum" 列下的文本出现问题,其中文本跨度太垂直,从而使每个单元格不必要地变大。我想同时修复它们,以便让 GridView 看起来不错。

C#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;


public partial class Maintenance : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataTable dt = new DataTable();
            ProductSetTableAdapters.Product_Table_AlphaTableAdapter productAdapter = new ProductSetTableAdapters.Product_Table_AlphaTableAdapter();
            ProductView.DataSource = productAdapter.GetData();
            ProductView.DataBind();
        }

        ProductView.RowDataBound += new GridViewRowEventHandler(ProductViewRowBound);
    }

    protected void ProductViewRowBound(object sender, GridViewRowEventArgs e)
    {
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
            string hyperLink = e.Row.Cells[1].Text;
            e.Row.Cells[1].Text = "";
            string id = e.Row.Cells[0].Text;
            string navigationURL = "Product_Maintenance.aspx?product=" + id;
            HyperLink link = new HyperLink { NavigateUrl = navigationURL, Text=hyperLink };
            e.Row.Cells[1].Controls.Add(link);

        e.Row.Cells[3].Wrap = false;  //Works but makes it so text spans horizontally as opposed to vertically
        e.Row.Cells[3].Attributes.Add("style", "word-wrap:break-word;"); //Does not work
        e.Row.Cells[3].Attributes.Add("style", "white-space:normal;"); //Does not work - Acts the same as if wrap was true
        e.Row.Cells[3].Width = new Unit("500px"); //Does not work
        e.Row.Cells[2].Width = new Unit("500px"); //Does not work

        ProductView.Columns[3].ItemStyle.Width = 50; //Throws ArgumentOutOfRange exception
        }
    }
}

ASPX 代码:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Maintenance.aspx.cs" Inherits="Maintenance" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <div style="margin-left:auto; margin-right:auto;">
        <asp:GridView ID="ProductView" runat="server" Height="299px" Width="863px" AllowPaging="True" HorizontalAlign="Center"
            OnRowDataBound="ProductViewRowBound">
        </asp:GridView>
    </div>
</asp:Content>

我正在使用TableAdapter 与我的 SQL Server 2017 express 数据库进行通信。此外,我正在使用自动生成的列,据我所知,在创建它们之前我无法指定样式。如何格式化列,以便文本可以很好地适应代码后面的 "Quick_Sum""Summary"

编辑 - 尝试了一些解决方案并更新了代码以反映这一点以及每个相应的结果。

EDIT 2 - 尝试了 wazz 的解决方案,它奏效了。在 aspx 文件中删除 GridView 的设置尺寸使得 RowDataBound 方法中的硬编码宽度调整产生明显的变化。

更新的 C# 代码:

protected void ProductViewRowBound(object sender, GridViewRowEventArgs e)
{

    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        string hyperLink = e.Row.Cells[1].Text;
        e.Row.Cells[1].Text = "";
        string id = e.Row.Cells[0].Text;
        string navigationURL = "Product_Maintenance.aspx?product=" + id;
        HyperLink link = new HyperLink { NavigateUrl = navigationURL, Text=hyperLink };
        e.Row.Cells[1].Controls.Add(link);

        e.Row.Cells[3].Wrap = false; 
        e.Row.Cells[3].Attributes.Add("style", "white-space:normal;"); //Removing this strangely makes it so that it looks like when wrap is on
        e.Row.Cells[3].Width = new Unit("500px"); //Now works
        e.Row.Cells[2].Width = new Unit("500px"); //Now works

    }
}

更新的 ASPX:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Maintenance.aspx.cs" Inherits="Maintenance" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <div style="margin-left:auto; margin-right:auto;">
        <asp:GridView ID="ProductView" runat="server" AllowPaging="True" HorizontalAlign="Center"
            OnRowDataBound="ProductViewRowBound" AutoGenerateColumns="True">
        </asp:GridView>
    </div>
</asp:Content>

感谢大家的帮助!

【问题讨论】:

  • 您在标记中硬编码这些宽度?
  • @IrishChieftain 是的,我在 RowDataBound 方法中对宽度进行了硬编码,因为据我所知,这就是我使用 AutoGeneratedColumns 的方法。有没有其他方法可以做到这一点,或者我对宽度进行了错误的硬编码?
  • 不在 RowDataBound 方法中,在标记中。
  • @IrishChieftain 你的意思是在 aspx 中吗?我不熟悉该术语,但如果您的意思是在 aspx 中,那么不,因为我已将 GridView 设置为在使用 TableAdapter 绑定数据时自动生成列。
  • @wazz 这似乎奏效了。您可能在设置高度和宽度的情况下是正确的,GridView 对列做了奇怪的事情以适应尺寸。删除高度和宽度使 RowDataBound 中硬编码的宽度调整产生了明显的变化。感谢您的帮助!

标签: c# css asp.net gridview


【解决方案1】:

首先在GridView1.SomeProperties... 和 RowDataBound 事件中提及 GridView 名称:

  • ItemStyle 属性设置Width 属性。

  • ItemStyle 属性的Wrap 属性设置为FALSE

    如果 Wrap 属性为 FALSE,则会自动调整列的大小。

GridView1.Columns[2].ItemStyle.Width = 50;
GridView1.Columns[2].ItemStyle.Wrap = false;

关注 MSDN:How to: Set GridView Web Server Control Column Width Dynamically

【讨论】:

  • 我试过了,它抛出了 ArgumentOutOfRangeException。我不明白它为什么会抛出这个异常。
  • 可能您输入的列号无效。
  • 我尝试使用 ProductView.Columns[0] ,它也抛出了同样的错误。我认为这是因为从技术上讲,我设置 GridView 的方式没有任何列。我将尝试重建我的 GridView 以便它使用 TemplateFields,然后再次尝试您的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-18
  • 2014-06-11
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-28
  • 2012-01-21
相关资源
最近更新 更多