如何设置在asp.net中动态生成的网格行的固定宽度和高度

时间:2012-12-07 11:31:09

标签: asp.net

我有一个asp.net网格视图。此行和列基于从数据库检索的值动态生成。在.aspx页面中我使用网格 <asp:GridView Height="250" Width="100%" runat="server" />

如果我有更多的行,那么网格就像下面一样。 enter image description here

如果我只有一行网格显示如下。 enter image description here

但是我想显示与第一张图像相同的网格行高度,即使有单行也是如此。 如何将网格行高度值设置为固定。我在stackoverflow中发现了类似的问题。但那些没有给我解决方案。

4 个答案:

答案 0 :(得分:4)

您可以使用GridView.RowStyle Property来定义行样式

GridView.RowStyle Property获取对TableItemStyle对象的引用,该对象使您能够在GridView控件中设置数据行的外观。

例如

<asp:GridView ID="GridView1">
        <rowstyle Height="20px" />
        <alternatingrowstyle  Height="20px"/>
</asp:GridView>

答案 1 :(得分:2)

CSS: / /

.MoGrid { width: 100%; background-color: #fff; margin: 5px 0 10px 0;}

.MoGrid td { color: #F05117;font-family: georgia;font-weight: bold;padding: 30px 2px 2px;}

.MoGrid th {border-bottom: 1px solid #F05117;border-top: 1px solid #F05117;color: #29B6EA;
    font-size: 13px;font-weight: bold;padding: 4px 2px;font-family: georgia;}

.MoGrid .alt { background: #fcfcfc url(../Styles/images/grd_alt.png) repeat-x top; }

.MoGrid .pgrM {background: #29B6EA; height:10px; }

.MoGrid .pgrM table { margin: 5px 0; }

.MoGrid .pgrM td { border-width: 0; padding: 0 6px; border-left: solid 1px #666; font-weight: bold; color: #fff; line-height: 12px; float:left; }  

.MoGrid .pgrM a { color: #666; text-decoration: none; }

.MoGrid .pgrM a:hover { font-family: tahoma;font-size: 12px;background-color: #99BBE1; }

消息来源:

<asp:GridView ID="GridView2" runat="server" CssClass="MoGrid"       
PagerStyle-CssClass="pgrM" AlternatingRowStyle-CssClass="alt">

答案 2 :(得分:2)

有点棘手,但对我有用。 您可以通过声明gridview本身的高度(以px为单位)来使gridview固定高度。现在,为了使行收缩到适当的高度(而不是展开以填充空白),将页脚样式的高度设置为100%。这样可以确保即使显示的记录数小于分页大小,页脚也会占用冗余,使数据行处于原始的“未展开”状态。

首先:

showfooter="true" 

在网格中。

并在footerstyle中:

  <footerstyle Height="100%" />

答案 3 :(得分:1)

您可以使用gridview的RowStyle-Height =“20”属性和此代码onPage_PreRender

例如

if(grdView.Rows.Count>0)
grdView.Height = new Unit(grdView.RowStyle.Height.Value * grdView.Rows.Count);

remove Height property from gridView

相关问题