首页 > 开发 > 综合 > 正文

如何控制DataGrid里的内容换行与不换行?

2024-07-21 02:23:11
字体:
来源:转载
供稿:网友
需求千奇百怪,有的要求表格里的内容自动换行,有的不要求,其实设置datagrid的css样式单属性即可实现大部分的需求,但对于不换行的实现,单靠样式单还不能完全满足要求,下面就是一种解决的方法:

<%@ page language="c#" autoeventwireup="true" %>
<%@ import namespace="system.data" %>
<html>
<script runat="server">
int start_index;
icollection createdatasource()
{
datatable dt = new datatable();
datarow dr;

dt.columns.add(new datacolumn("integervalue", typeof(int32)));
dt.columns.add(new datacolumn("stringvalue", typeof(string)));
dt.columns.add(new datacolumn("currencyvalue", typeof(double)));

for (int i = start_index; i < start_index + itemsgrid.pagesize; i++)
{
dr = dt.newrow();
dr[0] = i;
dr[1] = @"我是中文文字,i am english words,我不想换行,
i don't wanna have new lines,欢迎访问
<a href='http://dotnet.aspx.cc/'>http://dotnet.aspx.cc/</a>,
有好料啊:)";
dr[2] = 1.23 * (i+1);

dt.rows.add(dr);
}

dataview dv = new dataview(dt);
return dv;
}

void page_load(object sender, eventargs e)
{
//对于没有数字的内容,下面这行完全满足要求,但加了数字就不行,必须调用onitemdatabound
itemsgrid.attributes.add("style","word-break:keep-all;word-wrap:normal");

//下面这行是自动换行
//itemsgrid.attributes.add("style","word-break:break-all;word-wrap:break-word");

if (!ispostback)
{
bindgrid();
}
}

void bindgrid()
{
itemsgrid.datasource=createdatasource();
itemsgrid.databind();
}

void item_databound(object sender, datagriditemeventargs e)
{
if( e.item.itemtype == listitemtype.item ||
e.item.itemtype == listitemtype.alternatingitem)
e.item.cells[1].text = "<nobr>" + e.item.cells[1].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="{0:c}">
<itemstyle horizontalalign="right"></itemstyle>
</asp:boundcolumn>
</columns>

</asp:datagrid>
</form>
</body>
</html>

最大的网站源码资源下载站,

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表