<%@ Page Language="vb"%> <!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> ' 计算数据,这里可以适当修改从数据库中获取 PrivateFunction CreateDataSource()Function CreateDataSource() As System.Data.DataView Dim dt As System.Data.DataTable =New System.Data.DataTable Dim dr As System.Data.DataRow Dim i AsInteger dt.Columns.Add(New System.Data.DataColumn("学生班级", GetType(String))) dt.Columns.Add(New System.Data.DataColumn("学生姓名", GetType(String))) dt.Columns.Add(New System.Data.DataColumn("语文", GetType(String))) dt.Columns.Add(New System.Data.DataColumn("数学", GetType(String))) dt.Columns.Add(New System.Data.DataColumn("英语", GetType(String))) dt.Columns.Add(New System.Data.DataColumn("计算机", GetType(String))) For i =0To8 Dim rd AsNew System.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 Dim dv As System.Data.DataView =New System.Data.DataView(dt) Return dv End Function ' 设置每页显示的行数 Dim TotalRowCount AsInteger=12 ' 自动填充的行数 Dim numCount AsInteger=0 ProtectedSub Page_Load()Sub Page_Load(ByVal sender AsObject, ByVal e As EventArgs) IfNot Page.IsPostBack Then GridView1.DataSource = CreateDataSource() GridView1.DataBind() EndIf End Sub ProtectedSub GridView1_RowDataBound()Sub GridView1_RowDataBound(ByVal sender AsObject, ByVal e As GridViewRowEventArgs) If (e.Row.RowType = DataControlRowType.DataRow) Then numCount = numCount +1 EndIf If (e.Row.RowType = DataControlRowType.Footer) Then Dim toLeft AsInteger= TotalRowCount - numCount Dim numCols AsInteger= GridView1.Rows(0).Cells.Count Dim i, j AsInteger For i =0To toLeft Dim row As GridViewRow =New GridViewRow(-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal) For j =0To numCols Dim cell AsNew TableCell cell.Text =" " Next GridView1.Controls(0).Controls.AddAt(numCount +1+ i, row) Next EndIf End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>自动填充固定行数的 GridView</title> </head> <body> <form id="form1" runat="server"> <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" Font-Size="12px" CellPadding="3"> <HeaderStyle BackColor="#EDEDED"/> <Columns> <asp:TemplateField HeaderText="序号"> <ItemTemplate> <%#Eval("学生姓名") %> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </form> </body> </html>