熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

如何控制DataGrid裡的內容換行與不換行

2022-06-13   來源: Java核心技術 

  需求千奇百怪有的要求表格裡的內容自動換行有的不要求其實設置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>
  
  
  

From:http://tw.wingwit.com/Article/program/Java/hx/201311/26885.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.