Solution to the English line feed problem of DataGrid

Source: Internet
Author: User

When using the DataGrid two days ago, I encountered a lot of trouble. After several days of research, I finally got it done.CodePerformance is not very satisfactory.

The problem is that I used the DataGrid Control to bind a table in the database to it. At this time, I found that Chinese characters can be used for line breaks, but English cannot be used. How long is the display length in a row, in this way, the width of the entire DataGrid is long, so I checked msdn and found that this attribute can be explicitly set the DataGrid. columns [column Index]. width, my database has a total of more than a dozen fields. After binding with the DataGrid, IProgramWhen setting the above attribute, a runtime error occurs, saying that the index exceeds the boundary. I obtained the DataGrid. Columns. count in the program and found that this value is actually zero! This is so strange, because I have successfully bound and correctly displayed all the data in the database table, how can the number of columns of my DataGrid Control be zero! After checking the data online for a long time, we found from the msdn knowledge base that the default autogeneratecolumns attribute of the DataGrid is true, which means columns are automatically generated when data is bound by default, I set this attribute to false and use the following code to read data from the database.

Databasedomainmydbo = new databasepipeline ();
Mydbo. setdataadapter (SQL _string );
Mydbo. setdataset ();
Dataset myds = mydbo. getdataset ();
Datagrid1.width = unit. percentage (100 );
Int m = 0, n = 0;
Int colsize;
Int cposition = 0;
String rowtext;
String TMP = "";
// Char [] destination;
For (INT I = 1; I <myds. Tables [0]. Columns. Count; I ++)
// Create one column & add to DataGrid
Boundcolumn Col = new boundcolumn ();
Colsize = myds. Tables [0]. Columns [I]. columnname. length;
Col. headerstyle. width = colsize * 20;
Col. headerstyle. horizontalalign = horizontalalign. Center;
Col. headerstyle. Wrap = false;
// Col. itemstyle. width = unit. pixel (50 );
Col. itemstyle. Wrap = true;
Col. itemstyle. horizontalalign = horizontalalign. Left;
Col. headertext = myds. Tables [0]. Columns [I]. columnname. tostring ();

Col. datafield = myds. Tables [0]. Columns [I]. columnname. tostring ();
Col. sortexpression = myds. Tables [0]. Columns [I]. columnname. tostring ();

Datagrid1.columns. Add (COL );

Datagrid1.datasource = myds. Tables ["titles"]. defaultview;
Datagrid1.databind ();
Datagrid1.headerstyle. horizontalalign = horizontalalign. Center;
Mydbo. closeconn ();


In this case, I will check the DataGrid again. columns. the value of count is 14, which is the same as the number of fields in the database table bound to me. columns [column Index]. when the width is set to a fixed width, it can run normally without errors. However, this is only valid for Chinese characters. If a row contains long English letters, even if I set the column width, these long English texts will make it very wide. I have seen many people asking questions about the English line feed of the DataGrid Control on the Internet, I wrote the following code to solve the problem. The idea of this Code is as follows. After reading the data in each row in each column, I first check its length, if the column width is more than four times the configured width, I use <br> to cut the string into multiple segments. The maximum length of each segment cannot exceed four times the configured column width, then, when they are displayed on the page, they will wrap the line according to my requirements, that is, manually insert line breaks to the particularly long text.

// Process the English characters of the extra column
For (m = 0; m <myds. Tables [0]. Rows. Count; m ++)
// Tablecellcollection TB = new tablecellcollection ();
Cposition = 0;
TMP = "";
Datarow DR = myds. Tables [0]. Rows [m];
Rowtext = Dr [I]. tostring ();
If (Dr [I]. GetType (). Name = "string" & rowtext. length> colsize * 4)

// TMP = rowtext. substring (0, colsize * 4 );
// TMP = TMP + "...";
// Dr [I] = TMP;
For (n = 0; n <(rowtext. Length/(colsize * 4); N ++)
TMP = TMP + rowtext. substring (cposition, colsize * 4) + "<br> ";
Cposition = colsize * 4 * (n + 1 );
If (rowtext. Length % (colsize * 4) = 0)
Dr [I] = TMP;
TMP = TMP + rowtext. substring (cposition, (rowtext. Length % (colsize * 4 )));
Dr [I] = TMP;


This code solves the problem of line feed in English, but you can see that I have used a lot of nested for loops, so the execution performance is definitely not very good, hope that there are better solutions for people to give guidance, beginner ASP.. net.

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: 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.