简体   繁体   English

如何修改GridView的宽度?

[英]How to modify the width of the GridView?

I am using a GridView inside a Repeater control to generate three GridViews with 3 similar columns and different number of different columns between them. 我在Repeater控件内使用GridView来生成三个GridView,它们具有3个相似的列,并且它们之间具有不同数量的不同列。 Everything works fine and well. 一切正常,一切顺利。 I am just facing one probelm with the width of GridView. 我只是面对一个带有GridView宽度的探针。 The porblem is the following: 问题如下:

The second GridView has many columns and this is why its width goes outside the border of the page and this is also happened with the third gridview. 第二个GridView有很多列,这就是为什么它的宽度超出页面边界的原因,而第三个Gridview也是如此。 I solved this prblem by making the headers of each column to be displayed virtcally but the customer did not like it. 我通过使每列的标题以虚拟方式显示来解决了这个问题,但是客户不喜欢它。 Then, I tried to minimize the size of font and I reached to 7px and they said its too small. 然后,我尝试最小化字体大小,然后达到7px,他们说字体太小。 Now, I wondered what the solution to this problem. 现在,我想知道该问题的解决方案。 Any help please? 有什么帮助吗?

By the way, I don't have a problem to use the scroller from left to right, but if I can set the first four columns to be fixed. 顺便说一句,从左到右使用滚动条没有问题,但是如果可以将前四列设置为固定的话。 I don't know how to do that, too:). 我也不知道该怎么做:)。 So how to do it if it is this the only solution to this problem 那么,如果这是唯一解决此问题的方法,那么该怎么办

See the image below that shows the first GridView and the second one: 参见下图,其中显示了第一个GridView和第二个: 在此处输入图片说明

If you're using a div you could do this: 如果您使用的是div,则可以执行以下操作:

<div style="overflow:auto">
</div>

It will put horizontal scroll bars if the data in the gridview overflows 如果gridview中的数据溢出,它将放置水平滚动条

I've run into similar problems before and I've used varying solutions: 之前我也遇到过类似的问题,并且使用了各种解决方案:

1) Wrap your GridView in a div with an explicit width and ensure horizontal scrolling: 1)将GridView以明确的宽度包装在div中,并确保水平滚动:

<div style="width:700px; overflow:auto; overflow-y:hidden;">

2) Increase the width of your actual page. 2)增加实际页面的宽度。 I've used multiple masterpages with differing widths for normal and wider content and inherited accordingly. 对于正常和较宽的内容,我使用了多个宽度不同的母版页,并相应地进行了继承。

3) For appropriate columns, set the widths to something small (like 20-30px) and hide cell text content in your GridView's RowDataBound event. 3)对于适当的列,将宽度设置为较小的值(例如20-30px),并在GridView的RowDataBound事件中隐藏单元格文本内容。 Next, add a ToolTip containing the cell text on to the cell itself, optionally setting BackColor to indicate content and converting <br/> to Environment.NewLine or vice versa: 接下来,在单元格本身上添加一个包含单元格文本的工具提示,可以选择设置BackColor来指示内容,并将<br/>转换为Environment.NewLine ,反之亦然:

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState != DataControlRowState.Edit)
    {
        if (e.Row.Cells[0].Text != "")
        {
            e.Row.Cells[0].ToolTip = e.Row.Cells[0].Text.Replace(Environment.NewLine, "<br/>");
            e.Row.Cells[0].Text = "";
            e.Row.Cells[0].BackColor = Color.Lavender;
        }
    }
}

I appreciate this method may not help you as the column headers are also an issue. 我很欣赏这种方法可能对您没有帮助,因为列标题也是一个问题。

Regarding the fixed columns, the GridView control doesn't have inbuilt functionality for this but you can somewhat pull it off with CSS. 关于固定列,GridView控件没有内置功能,但是您可以使用CSS稍作改动。 I've used a class named 'locked' that I apply to GridView cells during RowDataBound, but the effects vary between browsers and it seems very hacky. 我使用了一个名为“锁定”的类,该类在RowDataBound期间应用于GridView单元,但是效果在不同的浏览器之间有所不同,而且看起来很hacky。

CSS 的CSS

td.locked, th.locked
{
    position:relative;
    left:expression(this.offsetParent.scrollLeft-3);
}

C# C#

e.Rows.Cells[0].CssClass = "locked"; e.Rows.Cells [0] .CssClass =“ locked”;

I suggest starting here http://forums.asp.net/t/1120278.aspx or Google 'freezing gridview columns in asp.net', then try to customise the method for your requirements. 我建议从这里http://forums.asp.net/t/1120278.aspx或Google'在asp.net中冻结gridview列',然后尝试根据您的要求自定义方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM