怎么样自动填充固定行数?
效果图:
代码:(C#)
<%@PageLanguage="C#"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<scriptrunat="server">
//计算数据,这里可以适当修改从数据库中获取
ICollectionCreateDataSource()
{
System.Data.DataTabledt=newSystem.Data.DataTable();
System.Data.DataRowdr;
dt.Columns.Add(newSystem.Data.DataColumn("学生班级",typeof(System.String)));
dt.Columns.Add(newSystem.Data.DataColumn("学生姓名",typeof(System.String)));
dt.Columns.Add(newSystem.Data.DataColumn("语文",typeof(System.Decimal)));
dt.Columns.Add(newSystem.Data.DataColumn("数学",typeof(System.Decimal)));
dt.Columns.Add(newSystem.Data.DataColumn("英语",typeof(System.Decimal)));
dt.Columns.Add(newSystem.Data.DataColumn("计算机",typeof(System.Decimal)));
for(inti=0;i<8;i++)
{
System.Randomrd=newSystem.Random(Environment.TickCount*i);;
dr=dt.NewRow();
dr[0]="班级"+i.ToString();
dr[1]=i.ToString();
dr[2]=System.Math.Round(rd.NextDouble()*100,2);
dr[3]=System.Math.Round(rd.NextDouble()*100,2);
dr[4]=System.Math.Round(rd.NextDouble()*100,2);
dr[5]=System.Math.Round(rd.NextDouble()*100,2);
dt.Rows.Add(dr);
}
System.Data.DataViewdv=newSystem.Data.DataView(dt);
returndv;
}
//设置每页显示的行数
intTotalRowCount=12;
//自动填充的行数
intnumCount=0;
protectedvoidPage_Load(objectsender,EventArgse)
{
if(!Page.IsPostBack)
{
GridView1.DataSource=CreateDataSource();
GridView1.DataBind();
}
}
protectedvoidGridView1_RowDataBound(objectsender,GridViewRowEventArgse)
{
if(e.Row.RowType==DataControlRowType.DataRow)
{
//计算自动填充的行数
numCount++;
}
if(e.Row.RowType==DataControlRowType.Footer)
{
//计算完毕,在此添加缺少的行
inttoLeft=TotalRowCount-numCount;
intnumCols=GridView1.Rows[0].Cells.Count;
for(inti=0;i<toLeft;i++)
{
GridViewRowrow=newGridViewRow(-1,-1,DataControlRowType.EmptyDataRow,DataControlRowState.Normal);
for(intj=0;j<numCols;j++)
{
TableCellcell=newTableCell();
cell.Text=" ";
row.Cells.Add(cell);
}
GridView1.Controls[0].Controls.AddAt(numCount+1+i,row);
}
}
}
</script>
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headid="Head1"runat="server">
<title>自动填充固定行数的GridView</title>
</head>
<body>
<formid="form1"runat="server">
<asp:GridViewID="GridView1"runat="server"OnRowDataBound="GridView1_RowDataBound"
Font-Size="12px"CellPadding="3">
<HeaderStyleBackColor="#EDEDED"/>
<Columns>
<asp:TemplateFieldHeaderText="序号">
<ItemTemplate>
<%#Eval("学生姓名")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<scriptrunat="server">
//计算数据,这里可以适当修改从数据库中获取
ICollectionCreateDataSource()
{
System.Data.DataTabledt=newSystem.Data.DataTable();
System.Data.DataRowdr;
dt.Columns.Add(newSystem.Data.DataColumn("学生班级",typeof(System.String)));
dt.Columns.Add(newSystem.Data.DataColumn("学生姓名",typeof(System.String)));
dt.Columns.Add(newSystem.Data.DataColumn("语文",typeof(System.Decimal)));
dt.Columns.Add(newSystem.Data.DataColumn("数学",typeof(System.Decimal)));
dt.Columns.Add(newSystem.Data.DataColumn("英语",typeof(System.Decimal)));
dt.Columns.Add(newSystem.Data.DataColumn("计算机",typeof(System.Decimal)));
for(inti=0;i<8;i++)
{
System.Randomrd=newSystem.Random(Environment.TickCount*i);;
dr=dt.NewRow();
dr[0]="班级"+i.ToString();
dr[1]=i.ToString();
dr[2]=System.Math.Round(rd.NextDouble()*100,2);
dr[3]=System.Math.Round(rd.NextDouble()*100,2);
dr[4]=System.Math.Round(rd.NextDouble()*100,2);
dr[5]=System.Math.Round(rd.NextDouble()*100,2);
dt.Rows.Add(dr);
}
System.Data.DataViewdv=newSystem.Data.DataView(dt);
returndv;
}
//设置每页显示的行数
intTotalRowCount=12;
//自动填充的行数
intnumCount=0;
protectedvoidPage_Load(objectsender,EventArgse)
{
if(!Page.IsPostBack)
{
GridView1.DataSource=CreateDataSource();
GridView1.DataBind();
}
}
protectedvoidGridView1_RowDataBound(objectsender,GridViewRowEventArgse)
{
if(e.Row.RowType==DataControlRowType.DataRow)
{
//计算自动填充的行数
numCount++;
}
if(e.Row.RowType==DataControlRowType.Footer)
{
//计算完毕,在此添加缺少的行
inttoLeft=TotalRowCount-numCount;
intnumCols=GridView1.Rows[0].Cells.Count;
for(inti=0;i<toLeft;i++)
{
GridViewRowrow=newGridViewRow(-1,-1,DataControlRowType.EmptyDataRow,DataControlRowState.Normal);
for(intj=0;j<numCols;j++)
{
TableCellcell=newTableCell();
cell.Text=" ";
row.Cells.Add(cell);
}
GridView1.Controls[0].Controls.AddAt(numCount+1+i,row);
}
}
}
</script>
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headid="Head1"runat="server">
<title>自动填充固定行数的GridView</title>
</head>
<body>
<formid="form1"runat="server">
<asp:GridViewID="GridView1"runat="server"OnRowDataBound="GridView1_RowDataBound"
Font-Size="12px"CellPadding="3">
<HeaderStyleBackColor="#EDEDED"/>
<Columns>
<asp:TemplateFieldHeaderText="序号">
<ItemTemplate>
<%#Eval("学生姓名")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
vb.net
<%@PageLanguage="vb"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<scriptrunat="server">
'计算数据,这里可以适当修改从数据库中获取
PrivateFunctionCreateDataSource()AsSystem.Data.DataView
DimdtAsSystem.Data.DataTable=NewSystem.Data.DataTable
DimdrAsSystem.Data.DataRow
DimiAsInteger
dt.Columns.Add(NewSystem.Data.DataColumn("学生班级",GetType(String)))
dt.Columns.Add(NewSystem.Data.DataColumn("学生姓名",GetType(String)))
dt.Columns.Add(NewSystem.Data.DataColumn("语文",GetType(String)))
dt.Columns.Add(NewSystem.Data.DataColumn("数学",GetType(String)))
dt.Columns.Add(NewSystem.Data.DataColumn("英语",GetType(String)))
dt.Columns.Add(NewSystem.Data.DataColumn("计算机",GetType(String)))
Fori=0To8
DimrdAsNewSystem.Random(Environment.TickCount*i)
dr=dt.NewRow
dr(0)="班级"+i.ToString()
dr(1)=i.ToString()
dr(2)=System.Math.Round(rd.NextDouble()*100,2)
dr(3)=System.Math.Round(rd.NextDouble()*100,2)
dr(4)=System.Math.Round(rd.NextDouble()*100,2)
dr(5)=System.Math.Round(rd.NextDouble()*100,2)
dt.Rows.Add(dr)
Next
DimdvAsSystem.Data.DataView=NewSystem.Data.DataView(dt)
Returndv
EndFunction
'设置每页显示的行数
DimTotalRowCountAsInteger=12
'自动填充的行数
DimnumCountAsInteger=0
ProtectedSubPage_Load(ByValsenderAsObject,ByValeAsEventArgs)
IfNotPage.IsPostBackThen
GridView1.DataSource=CreateDataSource()
GridView1.DataBind()
EndIf
EndSub
ProtectedSubGridView1_RowDataBound(ByValsenderAsObject,ByValeAsGridViewRowEventArgs)
If(e.Row.RowType=DataControlRowType.DataRow)Then
numCount=numCount+1
EndIf
If(e.Row.RowType=DataControlRowType.Footer)Then
DimtoLeftAsInteger=TotalRowCount-numCount
DimnumColsAsInteger=GridView1.Rows(0).Cells.Count
Dimi,jAsInteger
Fori=0TotoLeft
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<scriptrunat="server">
'计算数据,这里可以适当修改从数据库中获取
PrivateFunctionCreateDataSource()AsSystem.Data.DataView
DimdtAsSystem.Data.DataTable=NewSystem.Data.DataTable
DimdrAsSystem.Data.DataRow
DimiAsInteger
dt.Columns.Add(NewSystem.Data.DataColumn("学生班级",GetType(String)))
dt.Columns.Add(NewSystem.Data.DataColumn("学生姓名",GetType(String)))
dt.Columns.Add(NewSystem.Data.DataColumn("语文",GetType(String)))
dt.Columns.Add(NewSystem.Data.DataColumn("数学",GetType(String)))
dt.Columns.Add(NewSystem.Data.DataColumn("英语",GetType(String)))
dt.Columns.Add(NewSystem.Data.DataColumn("计算机",GetType(String)))
Fori=0To8
DimrdAsNewSystem.Random(Environment.TickCount*i)
dr=dt.NewRow
dr(0)="班级"+i.ToString()
dr(1)=i.ToString()
dr(2)=System.Math.Round(rd.NextDouble()*100,2)
dr(3)=System.Math.Round(rd.NextDouble()*100,2)
dr(4)=System.Math.Round(rd.NextDouble()*100,2)
dr(5)=System.Math.Round(rd.NextDouble()*100,2)
dt.Rows.Add(dr)
Next
DimdvAsSystem.Data.DataView=NewSystem.Data.DataView(dt)
Returndv
EndFunction
'设置每页显示的行数
DimTotalRowCountAsInteger=12
'自动填充的行数
DimnumCountAsInteger=0
ProtectedSubPage_Load(ByValsenderAsObject,ByValeAsEventArgs)
IfNotPage.IsPostBackThen
GridView1.DataSource=CreateDataSource()
GridView1.DataBind()
EndIf
EndSub
ProtectedSubGridView1_RowDataBound(ByValsenderAsObject,ByValeAsGridViewRowEventArgs)
If(e.Row.RowType=DataControlRowType.DataRow)Then
numCount=numCount+1
EndIf
If(e.Row.RowType=DataControlRowType.Footer)Then
DimtoLeftAsInteger=TotalRowCount-numCount
DimnumColsAsInteger=GridView1.Rows(0).Cells.Count
Dimi,jAsInteger
Fori=0TotoLeft
本文地址:http://www.45fan.com/dnjc/70472.html