1 <%@ Page Language="C#" AutoEventWireup="True" %>
2 <%@ Import Namespace="System.Data" %>
3 <html>
4 <script runat="server">
5 int start_index;
6 ICollection CreateDataSource()
7 {
8 DataTable dt = new DataTable();
9 DataRow dr;
10
11 dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
12 dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
13 dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));
14
15 for (int i = start_index; i < start_index + ItemsGrid.PageSize; i++)
16 {
17 dr = dt.NewRow();
18 dr[0] = i;
19 dr[1] = @"我是中文文字,I am English words,我不想换行, 20 I don't wanna have new lines,欢迎访问
21 <a href='http://dotnet.aspx.cc/'>http://dotnet.aspx.cc/</a>,
22 有好料啊:)";
23 dr[2] = 1.23 * (i+1);
24
25 dt.Rows.Add(dr);
26 }
27
28 DataView dv = new DataView(dt);
29 return dv;
30 }
31
32 void Page_Load(Object sender, EventArgs e)
33 {
34 //对于没有数字的内容,下面这行完全满足要求,但加了数字就不行,必须调用OnItemDataBound
35 ItemsGrid.Attributes.Add("style","word-break:keep-all;word-wrap:normal");
36
37 //下面这行是自动换行
38 //ItemsGrid.Attributes.Add("style","word-break:break-all;word-wrap:break-word");
39
40 if (!IsPostBack)
41 {
42 BindGrid();
43 }
44 }
45
46 void BindGrid()
47 {
48 ItemsGrid.DataSource=CreateDataSource();
49 ItemsGrid.DataBind();
50 }
51
52 void Item_DataBound(Object sender, DataGridItemEventArgs e)
53 {
54 if( e.Item.ItemType == ListItemType.Item ||
55 e.Item.ItemType == ListItemType.AlternatingItem)
56 e.Item.Cells[1].Text = "<nobr>" + e.Item.Cells[1].Text + "</nobr>";
57 }
58
59 </script>
60 <body>
61 <form runat="server">
62 <asp:DataGrid id="ItemsGrid" runat="server" BorderColor="black"
63 OnItemDataBound="Item_DataBound" AutoGenerateColumns="false">
64
65 <AlternatingItemStyle BackColor="#DEDEDE"></AlternatingItemStyle>
66 <HeaderStyle BackColor="#EEEEFF" HorizontalAlign="Center"></HeaderStyle>
67
68 <Columns>
69 <asp:BoundColumn HeaderText="序号" DataField="IntegerValue"/>
70 <asp:BoundColumn HeaderText="文字" DataField="StringValue"/>
71 <asp:BoundColumn HeaderText="价格" DataField="CurrencyValue" DataFormatString="{0:c}">
72 <ItemStyle HorizontalAlign="right"></ItemStyle>
73 </asp:BoundColumn>
74 </Columns>
75
76 </asp:DataGrid>
77 </form>
78 </body>
79 </html>




浙公网安备 33010602011771号