如何控制DataGrid里的内容换行与不换行?
2004-09-24 18:24
393 查看
如何控制DataGrid里的内容换行与不换行?
需求千奇百怪,有的要求表格里的内容自动换行,有的不要求,其实设置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>
相关文章推荐
- 如何控制DataGrid里的内容换行与不换行?(转)
- 如何控制DataGrid里的内容换行与不换行
- 如何控制DataGrid里的内容换行与不换行?
- 如何控制DataGrid里的内容换行与不换行?
- 如何控制DataGrid里的内容换行与不换行?
- 如何控制DataGrid里的内容换行与不换行?
- 如何控制DataGrid里的内容换行与不换行?
- 如何控制DataGrid里的内容换行与不换行?
- 如何控制DataGrid里的内容换行与不换行?
- 控制DATAGRID中内容如何换行
- 如何控制DataGrid里的内容换行与不换行?
- 如何控制DataGrid里的内容换行与不换行?
- 如何控制DataGrid里的内容换行与不换行?
- 为适应不同屏幕的宽度,datagrid用百分比设置宽度,无法事先知道各列的PX值,如何用样式控制内容的截取
- 控制DataGrid里的内容换行与不换行
- table 中,如何使得单元格的内容不换行,单元格不被撑开,显示不下的时候在结尾处显示“...”
- 【转】JAVA输出内容打印到TXT以及不同系统中如何换行
- html内容超出了div的宽度如何换行让内容自动换行
- Flex DataGrid内容自动换行相关
- 控制网页中表格内容自动换行