需求千奇百怪有的要求表格里的内容自动换行有的不要求其实设置DataGrid的CSS样式单属性即可实现大部分的需求但对于不换行的实现单靠样式单还不能完全满足要求下面就是一种解决的方法 <%@ Page Language=C# AutoEventWireup=True %> <%@ Import Namespace=SystemData %> <html> <script runat=server> int start_index; ICollection CreateDataSource() { DataTable dt = new DataTable(); DataRow dr; dtColumnsAdd(new DataColumn(IntegerValue typeof(Int))); dtColumnsAdd(new DataColumn(StringValue typeof(string))); dtColumnsAdd(new DataColumn(CurrencyValue typeof(double))); for (int i = start_index; i < start_index + ItemsGridPageSize; i++) { dr = dtNewRow(); dr[] = i; dr[] = @我是中文文字I am English words我不想换行 I dont wanna have new lines欢迎访问 <a >/</a> 有好料啊); dr[] = * (i+); dtRowsAdd(dr); } DataView dv = new DataView(dt); return dv; } void Page_Load(Object sender EventArgs e) { //对于没有数字的内容下面这行完全满足要求但加了数字就不行必须调用OnItemDataBound ItemsGridAttributesAdd(stylewordbreak:keepall;wordwrap:normal); //下面这行是自动换行 //ItemsGridAttributesAdd(stylewordbreak:breakall;wordwrap:breakword); if (!IsPostBack) { BindGrid(); } } void BindGrid() { ItemsGridDataSource=CreateDataSource(); ItemsGridDataBind(); } void Item_DataBound(Object sender DataGridItemEventArgs e) { if( eItemItemType == ListItemTypeItem || eItemItemType == ListItemTypeAlternatingItem) eItemCells[]Text = <nobr> + eItemCells[]Text + </nobr>; } </script> <body> <form runat=server> <asp:DataGrid id=ItemsGrid runat=server BorderColor=black OnItemDataBound=Item_DataBound AutoGenerateColumns=false> <AlternatingItemStyle BackColor=#DEDEDE></AlternatingItemStyle> <HeaderStyle BackColor=#EEEEFF HorizontalAlign=Center></HeaderStyle> <Columns> <asp:BoundColumn HeaderText=序号 DataField=IntegerValue/> <asp:BoundColumn HeaderText=文字 DataField=StringValue/> <asp:BoundColumn HeaderText=价格 DataField=CurrencyValue DataFormatString={:c}> <ItemStyle HorizontalAlign=right></ItemStyle> </asp:BoundColumn> </Columns> </asp:DataGrid> </form> </body> </html> |