如何设置 Gridview 的 BouldField 列的最大宽度?
我有一个设计缺陷,并且迫切需要帮助,因为我对 .NET 还很陌生。
我的页面上有一个 GridView,我的目标是限制每列的最大宽度。代码如下:
<asp:GridView ID="GridViewMessages" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ID" DataSourceID="LinqDataSourceMessages"
ForeColor="#333333" GridLines="None" Width="600px">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:CommandField ShowDeleteButton="true" ButtonType="Button" ControlStyle-BackColor="Red" />
<asp:CommandField ShowEditButton="True" ShowSelectButton="True" />
<asp:BoundField DataField="FromUser" HeaderText="Pengirim" InsertVisible="False"
ReadOnly="True" SortExpression="FromUser" ItemStyle-Width="10%" ItemStyle-Wrap="false" />
<asp:BoundField DataField="ToUser" HeaderText="Penerima" InsertVisible="False" ReadOnly="True"
SortExpression="ToUser" ItemStyle-Width="10%" ItemStyle-Wrap="false" />
<asp:BoundField DataField="Message1" HeaderText="Pesan" InsertVisible="False" ReadOnly="True"
SortExpression="Message1" HeaderStyle-Width="50%" HeaderStyle-Wrap="false" ItemStyle-Width="50%" ItemStyle-Wrap="false" />
<asp:CheckBoxField DataField="IsDone" HeaderText="Selesai?" SortExpression="IsDone"
ReadOnly="false" ItemStyle-Width="10%" ItemStyle-Wrap="false" />
<asp:BoundField DataField="DateCreated" HeaderText="Tanggal Buat" InsertVisible="False"
ReadOnly="True" SortExpression="DateCreated" ItemStyle-Width="20%" ItemStyle-Wrap="false" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
即使我使用 ItemStyle-Width="300px"
,如果数据长度大于 300px,它也不起作用。仅当每个数据的长度小于设置的宽度时,一切似乎才有效。
你们知道我该如何改变这个设计问题吗?
预先非常感谢您。
I have a design flaw and in a desperate need for a help because I am fairly new with .NET.
I have a GridView on my page and my goal is to limit the maximum width for each column. Below is the code:
<asp:GridView ID="GridViewMessages" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ID" DataSourceID="LinqDataSourceMessages"
ForeColor="#333333" GridLines="None" Width="600px">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:CommandField ShowDeleteButton="true" ButtonType="Button" ControlStyle-BackColor="Red" />
<asp:CommandField ShowEditButton="True" ShowSelectButton="True" />
<asp:BoundField DataField="FromUser" HeaderText="Pengirim" InsertVisible="False"
ReadOnly="True" SortExpression="FromUser" ItemStyle-Width="10%" ItemStyle-Wrap="false" />
<asp:BoundField DataField="ToUser" HeaderText="Penerima" InsertVisible="False" ReadOnly="True"
SortExpression="ToUser" ItemStyle-Width="10%" ItemStyle-Wrap="false" />
<asp:BoundField DataField="Message1" HeaderText="Pesan" InsertVisible="False" ReadOnly="True"
SortExpression="Message1" HeaderStyle-Width="50%" HeaderStyle-Wrap="false" ItemStyle-Width="50%" ItemStyle-Wrap="false" />
<asp:CheckBoxField DataField="IsDone" HeaderText="Selesai?" SortExpression="IsDone"
ReadOnly="false" ItemStyle-Width="10%" ItemStyle-Wrap="false" />
<asp:BoundField DataField="DateCreated" HeaderText="Tanggal Buat" InsertVisible="False"
ReadOnly="True" SortExpression="DateCreated" ItemStyle-Width="20%" ItemStyle-Wrap="false" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
Even if I use ItemStyle-Width="300px"
, it won't work if the data's length is bigger than 300px. Everything seems to work only if each data's length is less than the set width.
Do you guys know how can I alter this design problem?
Thank you so much in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想这是因为网格是用 HTML 表格呈现的。
在桌子上,所包含的总是获胜。如果列内的文本大于您声明的内容,则该列将被扩展。
I suppose it's because the grid is rendered with an HTML table.
In a table, the contained always wins. if the text inside the column is bigger than what you declare, the column will be expanded.