在有些开发实践中有时候为了页面美观的需要需要将GridView填充成固定行数的表格下面这个方法实现了这种填充的功能此方法可以处理自动产生列和模版列注意由于时间关系没有实现对完全空白数据的填充呵呵如果需要可以自己添加哈)! C# <%@PageLanguage=C#%> <!DOCTYPEhtmlPUBLIC//WC//DTDXHTMLTransitional//EN transitionaldtd> <scriptrunat=server> //计算数据完全可以从数据看取得 ICollectionCreateDataSourceByXianhuiMeng() { SystemDataDataTabledt=newSystemDataDataTable(); SystemDataDataRowdr; dtColumnsAdd(newSystemDataDataColumn(学生班级typeof(SystemString))); dtColumnsAdd(newSystemDataDataColumn(学生姓名typeof(SystemString))); dtColumnsAdd(newSystemDataDataColumn(语文typeof(SystemDecimal))); dtColumnsAdd(newSystemDataDataColumn(数学typeof(SystemDecimal))); dtColumnsAdd(newSystemDataDataColumn(英语typeof(SystemDecimal))); dtColumnsAdd(newSystemDataDataColumn(计算机typeof(SystemDecimal))); for(inti=;i<;i++) { SystemRandomrd=newSystemRandom(EnvironmentTickCount*i);; dr=dtNewRow(); dr[]=班级+iToString(); dr[]=【孟子E章】+iToString(); dr[]=SystemMathRound(rdNextDouble()*); dr[]=SystemMathRound(rdNextDouble()*); dr[]=SystemMathRound(rdNextDouble()*); dr[]=SystemMathRound(rdNextDouble()*); dtRowsAdd(dr); } SystemDataDataViewdv=newSystemDataDataView(dt); returndv; } //设置每页显示的行数 intTotalRowCount=; //自动填充的行数 intnumCount=; protectedvoidPage_Load(objectsenderEventArgse) { if(!PageIsPostBack) { GridViewDataSource=CreateDataSourceByXianhuiMeng(); GridViewDataBind(); } } protectedvoidGridView_RowDataBound(objectsenderGridViewRowEventArgse) { if(eRowRowType==DataControlRowTypeDataRow) { //计算自动填充的行数 numCount++; } if(eRowRowType==DataControlRowTypeFooter) { //计算完毕在此添加缺少的行 inttoLeft=TotalRowCountnumCount; intnumCols=GridViewRows[]CellsCount; for(inti=;i<toLeft;i++) { GridViewRowrow=newGridViewRow(DataControlRowTypeEmptyDataRowDataControlRowStateNormal); for(intj=;j<numCols;j++) { TableCellcell=newTableCell(); cellText= ; rowCellsAdd(cell); } GridViewControls[]ControlsAddAt(numCount++irow); } } } </script> <htmlxmlns=> <headrunat=server> <title>自动填充固定行数的GridView</title> </head> <body> <formid=formrunat=server> <asp:GridViewID=GridViewrunat=serverOnRowDataBound=GridView_RowDataBound FontSize=pxCellPadding=> <HeaderStyleBackColor=#EDEDED/> <Columns> <asp:TemplateFieldHeaderText=模版列> <ItemTemplate> <%#Eval(学生姓名)%> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </form> </body> </html> |