问题
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 . HTML does not deal with spaces in the way you would like, which is why you'll need the non-breaking-space ( ) character.
Or if you really would like to use a BoundField, modify your sql query to replace spaces with 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 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
to enter 3 spaces
来源:https://stackoverflow.com/questions/12938369/displaying-space-padded-records-in-asp-net-gridview