如果您喜欢在 gridview 上自动行,例如从数据库记录数中获取一些学生信息,那么这可能会有所帮助。
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="2" CellSpacing="1" DataKeyNames="Roll_No,ID_Number,Marks,Remarks" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Roll No">
<ItemTemplate>
<asp:Label ID="txtRoll_No" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID Number">
<ItemTemplate>
<asp:Label ID="txtID_Number" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Marks">
<ItemTemplate>
<asp:TextBox ID="txtMarks" runat="server" Width="50px" CssClass="text"
Text=""></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Remarks">
<ItemTemplate>
<asp:TextBox ID="txtRemarks" runat="server" CssClass="text" Width="200"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
VB.NET 代码
Dim dt As New DataTable()
Dim i As Integer
dt.Columns.Add(New DataColumn("Roll_No"))
dt.Columns.Add(New DataColumn("ID_Number"))
dt.Columns.Add(New DataColumn("Marks"))
dt.Columns.Add(New DataColumn("Remarks"))
For i = 1 To 10
Dim dRow As DataRow
dRow = dt.NewRow
dt.Rows.Add(dRow)
dt.AcceptChanges()
Next i
'now bind datatable to gridview...
GridView1.DataSource = dt
GridView1.DataBind()
With GridView1
For i = 0 To .Rows.Count - 1
Dim Roll_No As Label = DirectCast(.Rows(i).Cells(1).FindControl("txtRoll_No"), Label)
Dim ID_Number As Label = DirectCast(.Rows(i).Cells(2).FindControl("txtID_Number"), Label)
Roll_No.Text = i + 1
ID_Number.Text = "2014-01-" & i + 1
Next
End With
如果要逐行插入单行,则可以在其中编辑标记和备注以及 INSERT INTO DB,然后删除循环。