How do I control the contents of a DataGrid to wrap and not to change lines?

Source: Internet
Author: User
Datagrid| control Demand Strange, some requirements form the content of automatic line, some do not require, in fact, set the DataGrid CSS style single property can achieve most of the requirements, but for the implementation of the line, the style sheet can not fully meet the requirements, the following is a way to solve:

<%@ Page language= "C #" autoeventwireup= "True"%>
<%@ Import namespace= "System.Data"%>
<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 Chinese text, I am English words, I do not want to change lines,
I don ' t wanna have new lines, Welcome to visit
<a href= ' http://dotnet.aspx.cc/' >http://dotnet.aspx.cc/</a>
Have good material AH:) ";
DR[2] = 1.23 * (i+1);

Dt. Rows.Add (DR);
}

DataView dv = new DataView (DT);
return DV;
}

void Page_Load (Object sender, EventArgs e)
{
For content without numbers, the following line fully satisfies the requirements, but not with the addition of numbers, you must call Onitemdatabound
ITEMSGRID.ATTRIBUTES.ADD ("Style", "word-break:keep-all;word-wrap:normal");

The following line is a wrap line
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>

<Columns>
<asp:boundcolumn headertext= "serial number" datafield= "IntegerValue"/>
<asp:boundcolumn headertext= "text" datafield= "StringValue"/>
<asp:boundcolumn headertext= "Price" datafield= "Currencyvalue" dataformatstring= "{0:c}" >
<itemstyle horizontalalign= "right" ></ItemStyle>
</asp:BoundColumn>
</Columns>

</asp:DataGrid>
</form>
</body>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.