关于DataGrid的使用学习,DataGrid英文换行问题的解决,DataGrid如何指定各列的宽度

前两天使用DataGrid的过程中,遇到了很麻烦的几个地方,经过几天的研究总算基本搞定,但是个人对所写代码的性能不是很满意。

问题是这样的,我用DataGrid控件,将数据库中的某个表和它绑定,这个时候我发现中文可以换行,而英文却不能,有多长就在一行显示多长,这样将整个DataGrid的宽度撑的很长,于是我查了一下MSDN,发现有这样的属性可以显式的设定DataGrid.Columns[列索引].Width,我的数据库中共有十几个字段,和DataGrid绑定后,我在程序中设定上面的属性的时候,出现运行期错误,说索引超出边界,我又在程序中获取了一下DataGrid.Columns.Count,发现这个值居然是零!这就太奇怪了,因为我已经绑定成功,并且正确显示出了数据库表中的所有数据,那我的DataGrid控件的列数怎么可能是零呢!最后在网上查了很久,还是从MSDN知识库中发现DataGrid有一个属性AutoGenerateColumns默认为True,就是默认绑定数据的时候,自动产生列,我将这个属性设置为false,并用下面的代码自己读出数据库中的数据

DatabaseOper myDBO = new DatabaseOper();
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

 1<myds.tables[0].columns.count;i++) &="" add="" boundcolumn="" boundcolumn();="" col="new" col.datafield="myds.Tables[0].Columns[i].ColumnName.ToString();" col.headerstyle.horizontalalign="HorizontalAlign.Center;" col.headerstyle.width="colSize*20;" col.headerstyle.wrap="false;" col.headertext="myds.Tables[0].Columns[i].ColumnName.ToString();" col.itemstyle.horizontalalign="HorizontalAlign.Left;" col.itemstyle.width="Unit.Pixel(50);" col.itemstyle.wrap="true;" col.sortexpression="myds.Tables[0].Columns[i].ColumnName.ToString();" colsize="myds.Tables[0].Columns[i].ColumnName.Length;" column="" create="" datagrid="" datagrid1.columns.add(col);="" datagrid1.databind();="" datagrid1.datasource='myds.Tables["Titles"].DefaultView;' datagrid1.headerstyle.horizontalalign="HorizontalAlign.Center;" mydbo.closeconn();="" one="" to="" {="" }="" 这个时候我再查看datagrid.columns.count时,值是14,与我所绑定的数据库表中的字段数就一样了,我将datagrid.columns[列索引].width设置为某个固定的宽度的时候就能够正常运行,不出错了,但是这样仅对中文有效,如果某行中有很长英文的时候,即使我设定了列宽度,一样会被这些很长的英文文本撑的很宽,我看到网上有很多人问关于datagrid控件英文换行的问题,我写了下面的这段代码来解决问题,这段代码的思想是这样的,我将每一列中每行的数据读出来后,先检查其长度,如果超过我所设定的列宽的四倍时,我就将这个字符串用<br="">截成多段,每段的最大长度不超过设定列款的四倍,然后他们显示在页面的时候就会按我的要求换行了,即对特别长的文本做手工插入换行符的处理。 
 2
 3//处理超出的列款的英文字符   
 4for(m=0; m<myds.tables[0].rows.count;m++) &&="" ;="" cposition="0;" datarow="" dr="myds.Tables[0].Rows[m];" if(dr[i].gettype().name="String" rowtext="dr[i].ToString();" rowtext.length="" tablecellcollection="" tablecellcollection();="" tb="new" tmp="" {="">colSize*4)   
 5{   
 6  
 7// tmp = rowText.Substring(0,colSize*4);   
 8// tmp = tmp + "..." ;   
 9// dr[i] = tmp;   
10for(n=0;n&lt;(rowText.Length / (colSize*4));n++)   
11{   
12tmp=tmp + rowText.Substring(cPosition,colSize*4)+"<br/>";   
13cPosition=colSize*4*(n+1);   
14}   
15if((rowText.Length % (colSize*4))==0)   
16{   
17dr[i]=tmp;   
18}   
19else   
20{   
21tmp=tmp+rowText.Substring(cPosition,(rowText.Length % (colSize*4)));   
22dr[i]=tmp;   
23} 
24
25  
26}   
27} 
28
29这段代码很好的解决了英文换行的问题,但是大家看到了我用了很多嵌套的for循环,这样的执行性能肯定不是很好,希望有更好的解决方法的人给予指导,初学ASP.Net,贻笑大方了。</myds.tables[0].rows.count;m++)></myds.tables[0].columns.count;i++)>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus