C# 在asp.net gridview中禁用文本换行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15126215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:08:25  来源:igfitidea点击:

Disable text wrap in asp.net gridview

c#asp.netgridview

提问by user1954418

The output is like this:

输出是这样的:

MyNameIsJohnSmithAnd
Imaperson

What I want is to display it in only one line

我想要的是只在一行中显示它

MyNameIsJohnSmithAndImaperson

My Aspx gridview code is:

我的 Aspx gridview 代码是:

<asp:GridView ID="GridView1" runat="server" BackColor="LightGoldenrodYellow" 
    BorderColor="Tan" BorderWidth="1px" CellPadding="5" Font-Names="Calibri" 
    Font-Size="Medium" Font-Underline="False" ForeColor="Black">
    <RowStyle Wrap="False"/>
    <EmptyDataRowStyle Wrap="False"/>
    <FooterStyle BackColor="Tan" BorderColor="Black" BorderStyle="Solid" Wrap="False"/>
    <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" 
        HorizontalAlign="Center" Wrap="False" />
    <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" Wrap="False"/>
    <HeaderStyle BackColor="Tan" BorderStyle="Solid" Font-Bold="True" Wrap="False"/>
    <EditRowStyle Wrap="False"/>
    <AlternatingRowStyle BackColor="PaleGoldenrod" Wrap="False"/>
</asp:GridView>

I disabled all the wrap property to false in gridview. but the text still wraps.

我在 gridview 中将所有 wrap 属性禁用为 false。但文字仍然换行。

采纳答案by Freddie Fabregas

Try adding this event to your gridview.

尝试将此事件添加到您的 gridview。

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    for (int i = 0; i < e.Row.Cells.Count; i++)
    {
        e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
    }
}

Hereis the reference.

是参考。

回答by RMuesi

You have to set the gridview's individual column's word wrap to False as well.

您还必须将 gridview 的单个列的自动换行设置为 False。

<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" >
            <ItemStyle Wrap="False" />
            </asp:BoundField>

回答by user3578419

Very simple in .Net Select your grid view (as in Design mode) in property window and follow this RowStyle -->Font -->>Wrap=False

在 .Net 中非常简单 在属性窗口中选择您的网格视图(如在设计模式中)并遵循此 RowStyle -->Font -->>Wrap=False

Thats done

大功告成

回答by Musakkhir Sayyed

You can do this by css.

你可以通过 css 来做到这一点。

#GridView1
    {
      white-space:nowrap;
    }

回答by GrayDwarf

My column header text was wrapping so I ended up needing a slightly different solution but similar to what user3578419 suggested.

我的列标题文本正在换行,所以我最终需要一个稍微不同的解决方案,但类似于 user3578419 建议的解决方案。

In design view, I edited the 'ColumnHeadersDefaultCellStyle' property and changed the 'WrapMode' value to 'False'.

在设计视图中,我编辑了“ColumnHeadersDefaultCellStyle”属性并将“WrapMode”值更改为“False”。