一次更新Datagrid 中多列数据
在datagrid中一般我们只需一列列的更新数据,但有时需一次更新所有列中数据,提高数据编辑效率。
为了方便演示用xml文件做为数据源
xml文件:(UserInfo.xml)
<?xml version="1.0" standalone="yes"?>
<UserInfos>
<UserInfo>
<Email>some@you.net</Email>
<Name>me</Name>
<TEL>0123456</TEL>
</UserInfo>
<UserInfo>
<Email>youraddress@you.com</Email>
<Name>he</Name>
<TEL>7894563</TEL>
</UserInfo>
<UserInfo>
<Email>youraddress@you.com</Email>
<Name>222</Name>
<TEL>123342</TEL>
</UserInfo>
<UserInfo>
<Email>youraddress@you.com</Email>
<Name>haha</Name>
<TEL>3545445</TEL>
</UserInfo>
<UserInfo>
<Email>youraddress@you.com</Email>
<Name>sam</Name>
<TEL>879563</TEL>
</UserInfo>
</UserInfos>
页面代码:
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="DGrid" runat="server" ShowFooter="True" GridLines="None" CellPadding="2" AutoGenerateColumns="False">
<HeaderStyle Font-Bold="True"></HeaderStyle>
<Columns>
<asp:TemplateColumn HeaderText="名字">
<ItemTemplate>
<asp:TextBox id=Name runat="server" Width="109px" Text='<%# DataBinder.Eval(Container, "DataItem.Name") %>'>
</asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox id="NewName" runat="server" Width="109px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="电话">
<ItemTemplate>
<asp:TextBox id=TEL runat="server" Width="109px" Text='<%# DataBinder.Eval(Container, "DataItem.TEL") %>'>
</asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox id="NewTEL" runat="server" Width="109px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn 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="删除">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:CheckBox Runat="server" ID="chkDelete"></asp:CheckBox>
</ItemTemplate>
<FooterStyle HorizontalAlign="Center"></FooterStyle>
<FooterTemplate>
<asp:LinkButton Runat="server" Text="增加" CommandName="Add" ID="Linkbutton1"></asp:LinkButton>
</FooterTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
<asp:Button id="btnUpdate" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 208px" runat="server"
Text="更新"></asp:Button>
</form>
后台代码:
private DataSet dsUser;//定义dsUser
private void Page_Load(object sender, System.EventArgs e)
{
// 读取XML 文件.到dataset中
dsUser = new DataSet();
dsUser.ReadXml(Server.MapPath("UserInfo.xml"));
if (!Page.IsPostBack )
{
BindData();
}
}
private void BindData()
{
// datagrid绑定dataset
DataView dv = new DataView(dsUser.Tables["UserInfo"]);
DGrid.DataSource = dv;
DGrid.DataBind();
}
//把dataset保存到xml文件
private void Save()
{
dsUser.WriteXml(Server.MapPath("UserInfo.xml"));
}
private void DGrid_ItemCommand(object source , System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//增加一行到dataset中。使用了arrary结构
if (e.CommandName == "Add")
{
string[] sUser = {"", "", ""};
sUser[0] = ((TextBox)e.Item.FindControl("NewEmail")).Text;
sUser[1] = ((TextBox)e.Item.FindControl("NewName")).Text;
sUser[2] = ((TextBox)e.Item.FindControl("NewTEL")).Text;
dsUser.Tables["UserInfo"].Rows.Add(sUser);
Save();
}
BindData();
}
private void btnUpdate_Click(object sender, System.EventArgs e)
{
// 遍历datagrid中所有列.
foreach (DataGridItem di in DGrid.Items)
{
//不遍历datagrid中的 header or footer.
if (di.ItemType == ListItemType.Item || di.ItemType == ListItemType.AlternatingItem)
{
DataRow dr = dsUser.Tables["UserInfo"].Rows.Find(DGrid.DataKeys[di.ItemIndex]);
// 如果删除被选中.
if (((CheckBox)di.FindControl("chkDelete")).Checked)
{
dsUser.Tables["UserInfo"].Rows.Remove(dr);
}
else
{
// 更新
dr["Email"] = ((TextBox)di.FindControl("Email")).Text;
dr["Name"] = ((TextBox)di.FindControl("Name")).Text;
dr["TEL"] = ((TextBox)di.FindControl("TEL")).Text;
}
}
}
// 如果有改动
if (dsUser.HasChanges())
{
Save();
}
BindData();
}