Asp.net datagrid,设置列宽
Asp.net datagrid, set width of columns
我有以下 asp.net 表,使用数据网格创建:
<%@ Page Language="vb" MasterPageFile="~/Masterpage.master" AutoEventWireup="false" MaintainScrollPositionOnPostback="true" CodeFile="View.aspx.vb" Inherits="MDGRenewals.page_views" %>
<%@ Register Src ="~/Webcontrols/Admin/Users/RoleManager.ascx" tagprefix="mdg" TagName="rolemanager" %>
<asp:Content ID="Content1" ContentPlaceHolderID="contentMain" runat="server">
<form id="Form1" method="post" runat="server">
<p><asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False" CellPadding="3">
<ItemStyle CssClass="DGR_ITEM"></ItemStyle>
<HeaderStyle CssClass="DGR_HEADER"></HeaderStyle>
<Columns>
<asp:TemplateColumn HeaderText="Username">
<ItemTemplate>
<asp:Label id="Label1" runat="server" Text='<%#Eval("user_id")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Page">
<ItemTemplate>
<asp:Hyperlink id="Link1" runat="server" Text='<%#Container.DataItem("page_name")%>'></asp:Hyperlink>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="View Count">
<ItemTemplate>
<asp:Label id="Label3" runat="server" Text='<%#Container.DataItem("Count")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid></P>
</form>
</asp:Content>
我的问题是我希望能够设置列的宽度,但我无法做到这一点。
从代码可以看出,我试过了:
<ItemStyle Width="200px"></ItemStyle>
<HeaderStyle Width="200px"></HeaderStyle>
但它没有对此做出回应。
我编辑了 post 以包含 css 和建议使用 css:
.DGR_HEADER
{
font-weight: bolder;
font-size: 11px;
vertical-align: baseline;
width: 200px;
cursor: default;
color: black;
font-family: Tahoma, Arial, Verdana, Helvetica, sans-serif;
background-color: LightSteelBlue;
text-align: center;
text-decoration: none;
height:15px;
}
.DGR_ITEM
{
font-weight: normal;
font-size: xx-small;
width: 200px;
cursor: default;
color: black;
font-family: Tahoma, Arial, Verdana, Helvetica, sans-serif;
height: 20px;
background-color: silver;
text-align: left;
}
由于数据网格只是呈现普通 html <table>
,您可以使用 css:
table td {
width: 200px;
}
要引用单个 table,您可以将其直接放入 aspx
文件(或 ascx
或其他任何文件)
#<%= this.DataGrid1.ClientID %> td {
width: 200px;
}
试试这个。您可以像这样设置列宽
ItemStyle-Width="30"
<asp:TemplateColumn HeaderText="Username" ItemStyle-Width="30">
ItemStyle-Width 和 HeaderStyle 都对我不起作用...直到我将网格设置得足够宽以至于它 "room" 可以扩展相关列。
我一直将列宽设置为 400px,但没有任何改变!
然后我将我的 WHOLE GRID 设置为 4000px,然后 oila,现在该列实际上是 400px。所有的列都被压在一起,所以它不能增长那一列。
这可能不是每个人的答案,但如果您无法使这些属性起作用,请尝试一下。
我有以下 asp.net 表,使用数据网格创建:
<%@ Page Language="vb" MasterPageFile="~/Masterpage.master" AutoEventWireup="false" MaintainScrollPositionOnPostback="true" CodeFile="View.aspx.vb" Inherits="MDGRenewals.page_views" %>
<%@ Register Src ="~/Webcontrols/Admin/Users/RoleManager.ascx" tagprefix="mdg" TagName="rolemanager" %>
<asp:Content ID="Content1" ContentPlaceHolderID="contentMain" runat="server">
<form id="Form1" method="post" runat="server">
<p><asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False" CellPadding="3">
<ItemStyle CssClass="DGR_ITEM"></ItemStyle>
<HeaderStyle CssClass="DGR_HEADER"></HeaderStyle>
<Columns>
<asp:TemplateColumn HeaderText="Username">
<ItemTemplate>
<asp:Label id="Label1" runat="server" Text='<%#Eval("user_id")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Page">
<ItemTemplate>
<asp:Hyperlink id="Link1" runat="server" Text='<%#Container.DataItem("page_name")%>'></asp:Hyperlink>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="View Count">
<ItemTemplate>
<asp:Label id="Label3" runat="server" Text='<%#Container.DataItem("Count")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid></P>
</form>
</asp:Content>
我的问题是我希望能够设置列的宽度,但我无法做到这一点。
从代码可以看出,我试过了:
<ItemStyle Width="200px"></ItemStyle>
<HeaderStyle Width="200px"></HeaderStyle>
但它没有对此做出回应。
我编辑了 post 以包含 css 和建议使用 css:
.DGR_HEADER
{
font-weight: bolder;
font-size: 11px;
vertical-align: baseline;
width: 200px;
cursor: default;
color: black;
font-family: Tahoma, Arial, Verdana, Helvetica, sans-serif;
background-color: LightSteelBlue;
text-align: center;
text-decoration: none;
height:15px;
}
.DGR_ITEM
{
font-weight: normal;
font-size: xx-small;
width: 200px;
cursor: default;
color: black;
font-family: Tahoma, Arial, Verdana, Helvetica, sans-serif;
height: 20px;
background-color: silver;
text-align: left;
}
由于数据网格只是呈现普通 html <table>
,您可以使用 css:
table td {
width: 200px;
}
要引用单个 table,您可以将其直接放入 aspx
文件(或 ascx
或其他任何文件)
#<%= this.DataGrid1.ClientID %> td {
width: 200px;
}
试试这个。您可以像这样设置列宽
ItemStyle-Width="30"
<asp:TemplateColumn HeaderText="Username" ItemStyle-Width="30">
ItemStyle-Width 和 HeaderStyle 都对我不起作用...直到我将网格设置得足够宽以至于它 "room" 可以扩展相关列。
我一直将列宽设置为 400px,但没有任何改变!
然后我将我的 WHOLE GRID 设置为 4000px,然后 oila,现在该列实际上是 400px。所有的列都被压在一起,所以它不能增长那一列。
这可能不是每个人的答案,但如果您无法使这些属性起作用,请尝试一下。