GridView动态添加新行
gridview动态添加行的原理就是用datatable增加新列然后重新绑定;
设计源码
设计gridview代码
<asp:GridView ID="gvFrontendTypeSetting" runat="server" AutoGenerateColumns="false"
CssClass="table" OnRowDeleting="gvFrontendTypeSetting_RowDeleting">
<Columns>
<asp:TemplateField HeaderText="序号">
<ItemTemplate>
<div align="center">
<asp:TextBox ID="txtSerialNumber" runat="server" Width="20px" Text='<%#Eval("serialNumber")%>'
BorderStyle="None"></asp:TextBox>
</div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="商品筛选类型">
<ItemTemplate>
<asp:TextBox ID="txtType" runat="server" Text='<%#Eval("type")%>' BorderStyle="None"></asp:TextBox>
</ItemTemplate>
<ItemStyle Width="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="修改时间">
<ItemTemplate>
<div align="center">
<asp:Label runat="server" ID="lblModificationTime" Text='<%#Eval("modifiedTime")%>'
BorderStyle="None"></asp:Label>
</div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="修改人">
<ItemTemplate>
<div align="center">
<asp:Label runat="server" ID="lblModificationName" Text='<%#Eval("modifiedName")%>'
BorderStyle="None"></asp:Label>
</div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<div align="center">
<asp:Button ID="btnDel" runat="server" Text="删除" CommandName="delete" />
<asp:Button ID="btnEdit" runat="server" Text="修改" CommandName="Edit" />
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
首先我们需要在初始化的时候,新建table并同时新建一个列,以便在初始化的时候就有一个新列可使用
/// <summary>
/// Init the gvFrontendTypeSetting to show
/// </summary>
private void InitGVFrontendTypeSetting()
{
//Create a datatable instance
DataTable tbScratchCard = new DataTable();
//column one
DataColumn colSerialNumber = new DataColumn();
colSerialNumber.DataType = System.Type.GetType("System.Int32");
colSerialNumber.ColumnName = "serialNumber";
//column two
DataColumn colType = new DataColumn();
colType.DataType = System.Type.GetType("System.String");
colType.ColumnName = "type";
//column three
DataColumn colModifiedTime = new DataColumn();
colModifiedTime.DataType = System.Type.GetType("System.DateTime");
colModifiedTime.ColumnName = "modifiedTime";
//column four
DataColumn colModifiedName = new DataColumn();
colModifiedName.DataType = System.Type.GetType("System.String");
colModifiedName.ColumnName = "modifiedName";
//DataColumnCollection
DataColumn[] cols = { colSerialNumber, colType, colModifiedTime, colModifiedName };
//Add columns to the table tbScratchCard
tbScratchCard.Columns.AddRange(cols);
//Add a new row to the table tbScratchCard
tbScratchCard.Rows.Add(tbScratchCard.NewRow());
tbScratchCard.Rows[0][&
相关新闻>>
- 发表评论
-
- 最新评论 更多>>