是的,这是可能的。这只是 Victor 发布的代码示例。
<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="False" DataKeyNames="PartNumber" GridLines="Horizontal"
ForeColor="#333333" CellPadding="4" Width="800" PageSize="20" OnDataBound="gvPartDetails_DataBound" OnSelectedIndexChanged="gvPartDetails_SelectedIndexChanged" OnSorting="gvPartDetails_Sorting">
<Columns>
<asp:TemplateField HeaderText="#">
<ItemTemplate>
<asp:Label ID="lblNumber" runat="server" Text='<%# Bind("Number") %>' ToolTip='<%# Bind("Description") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="LOC 1">
<ItemTemplate>
<asp:TextBox ID="txt_Qty1" AutoPostBack="false" runat="server" MaxLength="5" Width="50" Text='<%# Bind("Qty1") %>'></asp:TextBox>
</ItemTemplate>
<HeaderStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="LOC 2">
<ItemTemplate>
<asp:TextBox ID="txt_Qty2" AutoPostBack="false" runat="server" MaxLength="5" Width="50" Text='<%# Bind("Qty2") %>'></asp:TextBox>
</ItemTemplate>
<HeaderStyle Wrap="False" />
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<span id="Empty">No Details For This Such and Such.</span>
<a href="javascript:showNewPartMPE();"><img src="Images/icons/table_add.png" style="border:0px" alt="Add New Part" /></a>
</EmptyDataTemplate>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#284775" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
<AlternatingRowStyle BackColor="Gainsboro" ForeColor="#284775" />
</asp:GridView>
然后后面的代码......这很粗糙......
private void ApplyChanges()
{
foreach (GridViewRow row in this.gvDetails.Rows)
{
//do something with cells and data objects
//and then save add to list, save, etc.
}
}
编辑
上面是您编辑数据的地方。您可以使用 DataTables、DataViews、DataSets,并且正如其他解决方案所示,您可以将网格绑定到对象列表。这应该根据系统中的数据对象模型以及如何生成行来确定。