您可以在頁眉和/或頁腳中顯示添加行,查看RowCreated -Event。
ASPX:
<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#3366CC"
BorderStyle="None" BorderWidth="1px" CellPadding="4" AllowSorting="True" AutoGenerateColumns="False"
ShowFooter="True" OnRowCommand="GridView1_RowCommand" OnRowCreated="GridView1_RowCreated">
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<RowStyle BackColor="White" ForeColor="#003399" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
<Columns>
<asp:BoundField DataField="PersonID" HeaderText="PersonID" />
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="Label1" Text='<%# Eval("Name") %>' runat="server"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:Button ID="BtnAdd" CommandName="ADD" runat="server" Text="Add" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
代碼隱藏:
Public Class GridViewAddRowTop
Inherits System.Web.UI.Page
Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.Header Then
Dim extraRow As New GridViewRow(1, -1, DataControlRowType.Header, DataControlRowState.Normal)
Dim emptyCell As New TableCell()
Dim tc As TableCell = New DataControlFieldCell(DirectCast(e.Row.Cells(0), DataControlFieldCell).ContainingField)
Dim BtnAdd As New Button()
Dim TxtName As New TextBox
emptyCell.Controls.Add(New LiteralControl(" "))
extraRow.Cells.Add(emptyCell)
TxtName.ID = "TxtName"
tc.Controls.Add(TxtName)
BtnAdd.ID = "BtnAdd"
BtnAdd.CommandName = "ADD"
BtnAdd.Text = "Add"
tc.Controls.Add(BtnAdd)
extraRow.Cells.Add(tc)
DirectCast(sender, GridView).Controls(0).Controls.Add(extraRow)
End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim tbl = getDataSource()
Me.GridView1.DataSource = tbl
Me.GridView1.DataBind()
End If
End Sub
Private Function getDataSource() As DataTable
Dim tbl = New DataTable()
Dim idColumn = New DataColumn("PersonID", GetType(Int32))
idColumn.AutoIncrement = True
tbl.Columns.Add(idColumn)
tbl.Columns.Add(New DataColumn("Name", GetType(String)))
For i As Int32 = 1 To 15
Dim newRow = tbl.NewRow
newRow("Name") = i & ".Name"
tbl.Rows.Add(newRow)
Next
Return tbl
End Function
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)
If e.CommandName.ToUpper = "ADD" Then
Dim tbl = getDataSource()
Dim newRow = tbl.NewRow
Dim bu As New Button
Dim txtName = DirectCast(DirectCast(e.CommandSource, WebControl).NamingContainer.FindControl("txtName"), TextBox)
newRow("Name") = txtName.Text
tbl.Rows.Add(newRow)
DirectCast(sender, GridView).DataSource = tbl
DirectCast(sender, GridView).DataBind()
End If
End Sub
End Class
注意:此示例將添加一個 「添加行」 到HeaderRow(頂部)和FooterRow(刪除如果需要,可以使用FooterTemplate)。如果您想將其添加到自動生成的標題列下面,我將不得不進一步瞭解它。由於此時RowCreated中不存在Header,因此我有問題在它下面添加新行。
是創建控件動態?看起來像它,如果它不能顯示在標題後,那麼它更好地使用headertemplate正確?那麼差異是什麼? –
@Abu Hamzah:使用HeaderTemplate將失去內置的排序容量([SortExpression](http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.gridview.sortexpression)上列的超鏈接。 ASPX))。這將不得不手動,而不是我的方法。 –
我不確定我能理解你可以改述 –