displaying space padded records in asp.net gridview

こ雲淡風輕ζ 提交于 2019-12-24 02:17:04

问题


I have a gridview control where I am displaying records from a dataset. The dataset contains records that are space padded as needed. I am trying to follow the solution in this post programatically change style (padding) of a column in ASP.NET GridView

However, when I display them in the gridview, the records show left aligned.

How can I display space padded records?

<asp:GridView runat="server" ID="gvCustomer" Visible="false"
    Width="350px" CellSpacing="1" ClientIDMode="Static" CellPadding="2" 
    AutoGenerateColumns="false" OnRowDataBound="gvCustomer_OnRowDataBound"
    ViewStateMode="Enabled" EmptyDataText="">
        <Columns>
            <asp:BoundField DataField="CustomerName" ControlStyle-Width="225px" HeaderStyle-HorizontalAlign="Center" HeaderText="Customers" />
        </Columns>
</asp:GridView>

回答1:


What you'll have to do is change your BoundField to be a TemplateField, and use a RowDataBound event to replace all spaces with &nbsp;. HTML does not deal with spaces in the way you would like, which is why you'll need the non-breaking-space (&nbsp;) character.

Or if you really would like to use a BoundField, modify your sql query to replace spaces with &nbsp; and bind.




回答2:


It looks like you are only setting the Header's alignment. You want to add:

ItemStyle-HorizontalAlign="Center"

to the asp:BoundField control




回答3:


HTML does not deal with whitespace in the way you appear to think it does. If you have multiple spaces between text or tags, the browser will ignore any extra whitespace.

In other words, the following will render on screen in exactly the same way

<b>Hello                     World</b>
<b>Hello World</b>

So, what I would suggest is you replace each of the leading spaces with the "no breaking space" character &nbsp; which the browser will take notice of.

Unfortunately what I can't do is give you an example of how you would do that with the <BoundField> in a GridView... as I simply have never used it, and so cannot comment.




回答4:


If you want to apply your css class, do this

            <asp:BoundField>
            <ItemStyle CssClass="MyCSSClass" />
            </asp:BoundField>



回答5:


on a boundfield mark it as HtmlEncode = false and use

&nbsp;&nbsp;&nbsp;

to enter 3 spaces



来源:https://stackoverflow.com/questions/12938369/displaying-space-padded-records-in-asp-net-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!