Protected Sub newLine(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.DataBound Dim i As Integer Dim j As Integer j = GridView1.Rows.Count - 1 For i = 0 To j GridView1.Rows.Item(i).Cells(0).Text = GridView1.Rows.Item(i).Cells(0).Text.Replace(" ", "<br />") Next i End Sub//Cells(0)為row裡column 0的欄位值
<asp:GridView ID="GridView4" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource4"
OnSelectedIndexChanged="GridView4_SelectedIndexChanged" Width="100%">
<Columns>
<asp:BoundField DataField="Catalogue" SortExpression="Catalogue">
<ItemStyle Font-Size="Small" />
</asp:BoundField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:Graduation_ProjectConnectionString %>"
SelectCommand="SELECT [Catalogue] FROM [Book]WHERE ([Isbn] = @Isbn)">
<SelectParameters>
<asp:SessionParameter Name="Isbn" SessionField="Isbn" />
</SelectParameters>
</asp:SqlDataSource>
strapp=strtemp.Replace("\t\n","");
strapp=strtemp.Replace("\n", "");
二种:这种不是根据空格来换行,它是根据自身的宽度来换的。
style="word-wrap:break-word"
在绑定到GridView时,我想让此单元格这样显示:
aaa
bbb
ccc
你哪些空格是真的空格,你哪些空格是要轉行呢?你在DB中存的都是一樣啊.
你在存DB的時候就應該將換行符替換掉,如string.Replace("\r\n","[0]").
然后讀出來的時候你就可以在Gridview_RowDataBind里將[0]再替換成\r\n了
给你个我写的方法,适用gridview
/// <summary>
/// 数据阶段处理函数
/// </summary>
/// <param name="table">需要处理的DataTable</param>
/// <param name="ColName">需要处理的列名</param>
/// <param name="lenght">需要保留的长度</param>
/// <returns></returns>
public static DataTable InterceptColTable(DataTable table, string ColName, int lenght)
{
DataTable myTable = new DataTable();
myTable = table.Clone();
DataRow row;
for (int i = 0; i < table.Rows.Count; i++)
{
row = myTable.NewRow();
for (int j = 0; j < table.Columns.Count; j++)
{
if (table.Columns[j].ColumnName.Equals(ColName))
{
if (table.Rows[i][j].ToString().Length > lenght)
{
row[j] = table.Rows[i][j].ToString().Substring(0, lenght - 3) + "...";
}
else
{
row[j] = table.Rows[i][j];
}
}
else
{
row[j] = table.Rows[i][j];
}
}
myTable.Rows.Add(row);
}
return myTable;
}
<asp:GridView ID="GridView4" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource4"
OnSelectedIndexChanged="GridView4_SelectedIndexChanged" Width="100%">
<Columns>
<asp:TemplateField SortExpression="Catalogue">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Catalogue").ToString().Replace("\n", "<br />"); %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Catalogue").ToString().Replace("\n", "<br />"); %>'></asp:Label>
</ItemTemplate>
<ItemStyle Font-Size="Small" />
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:Graduation_ProjectConnectionString %>"
SelectCommand="SELECT [Catalogue] FROM [Book]WHERE ([Isbn] = @Isbn)">
<SelectParameters>
<asp:SessionParameter Name="Isbn" SessionField="Isbn" />
</SelectParameters>
</asp:SqlDataSource>也就是给要绑定的字段加上ToString().Replace("\n", "<br />");
gridview里有一列绑定的数据很长,显示的时候在一行里面显示,页面拉得很宽。
原因是连续英文段为一个整体导致的,在RowDataBound中添加上了一句e.Row.Cells[2].Style.Add("word-break", "break-all")就可以。如果要给所有的列增加此属性:
protected void Page_Load(object sender, EventArgs e)
{
//正常换行
GridView1.Attributes.Add("style", "word-break:keep-all;word-wrap:normal");
//下面这行是自动换行
GridView1.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
if (!IsPostBack)
{
bind();//调用数据绑定即可
}
}
总之:善用CSS的word-break:break-all;word-wrap:break-word属性即可,这个属性是通用的对于顽固的南换行问题都可以解决,不局限于GridView。
gv.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
<asp:GridView ID="GridView4" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource4"
OnSelectedIndexChanged="GridView4_SelectedIndexChanged" Width="100%">
<Columns>
<asp:TemplateField SortExpression="Catalogue">
<ItemTemplate><div>
<%# Eval("Catalogue").ToString().Replace("\n", "<br />") %>'>
</div>
</ItemTemplate>
<ItemStyle Font-Size="Small" />
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:Graduation_ProjectConnectionString %>"
SelectCommand="SELECT [Catalogue] FROM [Book]WHERE ([Isbn] = @Isbn)">
<SelectParameters>
<asp:SessionParameter Name="Isbn" SessionField="Isbn" />
</SelectParameters>
</asp:SqlDataSource>
呵呵
刚刚没测试来
Dim i As Integer
Dim j As Integer
j = GridView1.Rows.Count - 1
For i = 0 To j
GridView1.Rows.Item(i).Cells(0).Text = GridView1.Rows.Item(i).Cells(0).Text.Replace(" ", "<br />")
Next i
End Sub//Cells(0)為row裡column 0的欄位值