创建完全可编辑的 DataGrid (1)
点击次数:26 次 发布日期:2008-11-26 14:10:03 作者:源代码网
|
源代码网推荐 在论坛中我看到过许多相同或相似的问题:我怎样在我的DataGrid的每一行中放置检查框、文本框等等?怎样更新它们的值?答案相当简单,在这篇文章中,我将向你展示如何完成它。 源代码网推荐 源代码网推荐 我们都知道,DataGrid是一个功能非常强大的工具。根据我的经验,在90%以上的时间中, DataGrid 都被用来显示数据,并可能一次编辑一行数据。 而某些时候,可能需要一次编辑多行,甚至是所有数据。一个实际的例子就是在网上销售物品的应用程序中, 顾客可能一次要变更他们篮子中的一种或多种物品,单击检查框移去他们不想要的商品。 源代码网推荐 源代码网推荐 构想 源代码网推荐 源代码网推荐 在这个例子中,我写了一个简单的WebForm来管理存储在XML中的联系人列表。 这个需求是非常简单的:具有添加新联系人,编辑/删除现有联系人的能力。用户可以一次修改或删除多个联系人,我也允许用户按他们选定的列来对数据网格进行排序。 源代码网推荐 源代码网推荐 我的例子是用 C# 编写的。 如果你更喜欢这些代码的VB版本,在下载文件中有这两种格式的代码。 源代码网推荐 源代码网推荐 Contacts.xml 源代码网推荐 源代码网推荐 这个例子中的 XML 数据文件非常简单直观。由于它非常简单,所以我没有创建规划。 源代码网推荐 源代码网推荐 <?xml version="1.0" standalone="yes"?> 源代码网推荐 <Contacts> 源代码网推荐 <Contact> 源代码网推荐 <Email>myaddress@mycompany.com</Email> 源代码网推荐 <FirstName>John</FirstName> 源代码网推荐 <LastName>Doe</LastName> 源代码网推荐 </Contact> 源代码网推荐 <Contact> 源代码网推荐 <Email>youraddress@yourcompany.com</Email> 源代码网推荐 <FirstName>Jane</FirstName> 源代码网推荐 <LastName>Doe</LastName> 源代码网推荐 </Contact> 源代码网推荐 </Contacts> 源代码网推荐 源代码网推荐 ContactList.aspx 源代码网推荐 源代码网推荐 设置 WebForm 非常简单。我放置了一个新的 DataGrid 到我的窗体中,并且设置它为4列,第一列都包含了用来删除联系人的检查框。你会注意到我在这里做的主要工作就是以模板列( TemplateColumn)的形式创建了每一列。 这允许我放置文本框和检查框对象到数据模板(ItemTemplate)中 . 这是一个在网格每一行中显示文本以外的其它东西的技巧。 除此以外,你还会注意到我使用 FooterTemplate 来使新建联系人变得简单而直观。 源代码网推荐 源代码网推荐 我也包含了一个链接按钮(LinkButton),用来保存用户所做的修改及删除操作。但它并不用来添加新联系人。添加新联系人的操作由最后一列的页脚模板中链接按钮(LinkButton)来完成。 源代码网推荐 源代码网推荐 <asp:datagrid id="dgContacts" runat="server" ShowFooter="True" AllowSorting="True" Forefont color="Black" GridLines="None" CellPadding="2" Backfont color="LightGoldenrodYellow" BorderWidth="1px" Borderfont color="Tan" Width="499px" AutoGenerateColumns="False" DataKeyField="Email"> 源代码网推荐 <SelectedItemStyle Forefont color="GhostWhite" Backfont color="DarkSlateBlue"></SelectedItemStyle> 源代码网推荐 <AlternatingItemStyle Backfont color="PaleGoldenrod"></AlternatingItemStyle> 源代码网推荐 <HeaderStyle Font-Bold="True" Backfont color="Tan"></HeaderStyle> 源代码网推荐 <FooterStyle Backfont color="Tan"></FooterStyle> 源代码网推荐 <Columns> 源代码网推荐 <asp:TemplateColumn SortExpression="FirstName" HeaderText="First Name"> 源代码网推荐 <ItemTemplate> 源代码网推荐 <asp:TextBox id=First runat="server" Width="109px" Text="<%# DataBinder.Eval(Container, "DataItem.FirstName") %>"> 源代码网推荐 </asp:TextBox> 源代码网推荐 </ItemTemplate> 源代码网推荐 <FooterTemplate> 源代码网推荐 <asp:TextBox id="NewFirst" runat="server" Width="109px"></asp:TextBox> 源代码网推荐 </FooterTemplate> 源代码网推荐 </asp:TemplateColumn> 源代码网推荐 <asp:TemplateColumn SortExpression="LastName" HeaderText="Last Name"> 源代码网推荐 <ItemTemplate> 源代码网推荐 <asp:TextBox id=Last runat="server" Width="109px" Text="<%# DataBinder.Eval(Container, "DataItem.LastName") %>"> 源代码网推荐 </asp:TextBox> 源代码网推荐 </ItemTemplate> 源代码网推荐 <FooterTemplate> 源代码网推荐 <asp:TextBox id="NewLast" runat="server" Width="109px"></asp:TextBox> 源代码网推荐 </FooterTemplate> 源代码网推荐 </asp:TemplateColumn> 源代码网推荐 <asp:TemplateColumn SortExpression="Email" HeaderText="Email"> 源代码网推荐 <ItemTemplate> 源代码网推荐 <asp:TextBox id=Email runat="server" Text="<%# DataBinder.Eval(Container, "DataItem.Email") %>"> 源代码网推荐 </asp:TextBox> 源代码网推荐 </ItemTemplate> 源代码网推荐 <FooterTemplate> 源代码网推荐 <asp:TextBox id="NewEmail" runat="server"></asp:TextBox> 源代码网推荐 </FooterTemplate> 源代码网推荐 </asp:TemplateColumn> 源代码网推荐 <asp:TemplateColumn HeaderText="Delete Contact"> 源代码网推荐 <ItemStyle HorizontalAlign="Center"></ItemStyle> 源代码网推荐 <ItemTemplate> 源代码网推荐 <asp:CheckBox Runat="server" ID="chkDelete"></asp:CheckBox> 源代码网推荐 </ItemTemplate> 源代码网推荐 <FooterStyle HorizontalAlign="Center"></FooterStyle> 源代码网推荐 <FooterTemplate> 源代码网推荐 <asp:LinkButton Runat="server" Text="Add" CommandName="Add" ID="Linkbutton1" NAME="Linkbutton1"></asp:LinkButton> 源代码网推荐 </FooterTemplate> 源代码网推荐 </asp:TemplateColumn> 源代码网推荐 </Columns> 源代码网推荐 </asp:datagrid> 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
