创建完全可编辑的 DataGrid
在论坛中我看到过许多相同或相似的问题:我怎样在我的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>
contactlist.cs
当我选择用xml文件来存取数据后,我就决定要使用dataset来存取它。因为 dataset 对象有 readxml 和 writexml 方法,所以这是非常合理的选择。第一步是在xml中读取数据。正如你从代码中所看到的, 我创建了一个成员用来处理数据排序。
private dataset _dscontacts;private string _ssort;
private void page_load(object sender, system.eventargs e)
{ // 装载 xml 文件。
_dscontacts = new dataset();_dscontacts.readxml(server.mappath("contacts.xml"));datacolumn[] dcpk = {_dscontacts.tables["contact"].columns["email"]};_dscontacts.tables["contact"].primarykey = dcpk;
if (!page.ispostback )