如何GridView实现添加删除更新行?
数据后台为:xml文件<?xml version="1.0" standalone="yes"?>
<root>
<Mytable>
<气象名称>最大风</气象名称>
<风速v>25</风速v>
<冰厚b>0</冰厚b>
<温度t>5</温度t>
</Mytable>
<Mytable>
<气象名称>最厚冰</气象名称>
<风速v>0</风速v>
<冰厚b>15</冰厚b>
<温度t>-5</温度t>
</Mytable>
<Mytable>
<气象名称>最低温</气象名称>
<风速v>0</风速v>
<冰厚b>0</冰厚b>
<温度t>-20</温度t>
</Mytable>
<Mytable>
<气象名称>年均温</气象名称>
<风速v>0</风速v>
<冰厚b>0</冰厚b>
<温度t>10</温度t>
</Mytable>
<Mytable>
<气象名称>最高温</气象名称>
<风速v>0</风速v>
<冰厚b>0</冰厚b>
<温度t>40</温度t>
</Mytable>
<Mytable>
<气象名称>内过电压</气象名称>
<风速v>15</风速v>
<冰厚b>0</冰厚b>
<温度t>15</温度t>
</Mytable>
<Mytable>
<气象名称>外过电压</气象名称>
<风速v>10</风速v>
<冰厚b>0</冰厚b>
<温度t>15</温度t>
</Mytable>
<Mytable>
<气象名称>外过无风</气象名称>
<风速v>0</风速v>
<冰厚b>0</冰厚b>
<温度t>15</温度t>
</Mytable>
<Mytable>
<气象名称>外过有风</气象名称>
<风速v>10</风速v>
<冰厚b>0</冰厚b>
<温度t>15</温度t>
</Mytable>
<Mytable>
<气象名称>安装有风</气象名称>
<风速v>10</风速v>
<冰厚b>0</冰厚b>
<温度t>-10</温度t>
</Mytable>
<Mytable>
<气象名称>事故气象</气象名称>
<风速v>0</风速v>
<冰厚b>0</冰厚b>
<温度t>15</温度t>
</Mytable>
</root>
前台页;
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" BorderStyle="Double"
OnRowDeleting="GridView1_RowDeleting"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating"
DataKeyNames="气象名称" OnRowCancelingEdit="GridView1_RowCancelingEdit" >
<PagerSettings Visible="False" />
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ItemStyle-Width=200/>
</Columns>
<HeaderStyle Font-Size="10pt" />
</asp:GridView>
</div>
</form>
</body>
</html>
后台页:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Default2 : System.Web.UI.Page
{
DataSet mydata = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
mydata.ReadXml(Server.MapPath("Climate.xml"));
this.GridView1.DataSource = mydata.Tables["Mytable"].DefaultView;
this.GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
mydata.WriteXml(Server.MapPath("Climate.xml"));
}
/// <summary>
///
/// </summary>
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow gvr = this.GridView1.Rows[e.RowIndex];
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView1.EditIndex = e.NewEditIndex;
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow gvr = this.GridView1.Rows[e.RowIndex];
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1;
}
}
后台代码 ,没有写完,如何写?
--------------------编程问答-------------------- 前台的错了一点
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ItemStyle-Width=200/>
</Columns>
改为
<Columns>
<asp:CommandField CancelText="取消" UpdateText="更新" EditText="编辑" DeleteText="删除" ShowDeleteButton="True"
ShowEditButton="True" />
</Columns> --------------------编程问答-------------------- 删除xml?请使用XmlDocument类的方法 --------------------编程问答-------------------- --------------------编程问答-------------------- 顶起 --------------------编程问答-------------------- 孟子好多的牌牌撒
一般是自定义各linkbutton列,在rowcommand中写的,感觉那样比较灵活 --------------------编程问答-------------------- 没用过xml数据库,帮不上忙啦,哈哈哈,学习。 --------------------编程问答-------------------- Update功能:
可以取得每一行中对应的列的值,(TextBox)gr.Cells[gridview的列索引].Controls[0]).Text;
至于要更新到xml文件中去,就是楼上说的用xmldocument类,你要找到对应的节点,然后用取得的值去更新。 --------------------编程问答-------------------- 对xml文件进行操作,之后重新绑定~ --------------------编程问答-------------------- xml学习中. --------------------编程问答--------------------
.. --------------------编程问答-------------------- XmlDocument --------------------编程问答-------------------- 更新时采用没有追加的写XML文件
例子
///<summary>
/// 利用XmlTextWriter不容易实现追加,也就是说每次写入XML文档的数据都是覆盖式的,不容易实现在原有数据的基础上追加数据,
/// 而采用XmlNode模式就能方便地实现数据追加
/// 利用DataSet也不是追加数据
///</summary>
//利用DataSet把数据写入XML文件,不实现追加
public static string SaveXML_DataSet(BaseInfo.Phonebook pb)
{
try
{
DataTable dt = new DataTable();
dt.Columns.Add("name", Type.GetType("System.String"));
dt.Columns.Add("telphone", Type.GetType("System.String"));
dt.Columns.Add("qq", Type.GetType("System.String"));
dt.Columns.Add("msn", Type.GetType("System.String"));
dt.Columns.Add("cellphone", Type.GetType("System.String"));
dt.Columns.Add("workplace", Type.GetType("System.String"));
dt.Columns.Add("address", Type.GetType("System.String"));
dt.Columns.Add("email", Type.GetType("System.String"));
DataRow dr = dt.NewRow();
dr[0] = pb.name;
dr[1] = pb.telphone;
dr[2] = pb.qq;
dr[3] = pb.msn;
dr[4] = pb.cellphone;
dr[5] = pb.workplace;
dr[6] = pb.address;
dr[7] = pb.email;
dt.Rows.Add(dr);
System.Data.DataSet ds = new DataSet();
ds.Tables.Add(dt);
//将DataSet里面的数据写入XML
ds.WriteXml(System.Web.HttpContext.Current.Server.MapPath("phonebook.xml"));
return "true";
}
catch(Exception ex)
{
return ex.ToString();
}
}
//利用xmlTextWriter往XML文件写入数据,没有实现追加
public static string SaveXMLData_XmlTextWriter(BaseInfo.Phonebook pb)
{
try
{
//创建一个XmlTextWriter类的实例对象
System.IO.FileInfo file = new FileInfo(System.Web.HttpContext.Current.Server.MapPath("phonebook.xml"));
//if (!file.Exists)
XmlTextWriter textWriter = new XmlTextWriter(System.Web.HttpContext.Current.Server.MapPath("phonebook.xml"), System.Text.Encoding.UTF8);
//开始写过程,调用WriterStartDocument方法写入文件头信息
//例如<?xml version="1.0" encoding="utf-8"?>
textWriter.WriteStartDocument();
//写入根节点<phonebook></phonebook>
textWriter.WriteStartElement("phonebook");
textWriter.WriteStartElement("member");
//写入节点name
textWriter.WriteStartElement("name", "");
//为name节点写入用户输入的值
textWriter.WriteString(pb.name);
//写入name节点的结束符
textWriter.WriteEndElement();
textWriter.WriteStartElement("telphone", "");
textWriter.WriteString(pb.telphone);
textWriter.WriteEndElement();
textWriter.WriteStartElement("qq", "");
textWriter.WriteString(pb.qq);
textWriter.WriteEndElement();
textWriter.WriteStartElement("msn", "");
textWriter.WriteString(pb.msn);
textWriter.WriteEndElement();
textWriter.WriteStartElement("cellphone", "");
textWriter.WriteString(pb.cellphone);
textWriter.WriteEndElement();
textWriter.WriteStartElement("workplace", "");
textWriter.WriteString(pb.workplace);
textWriter.WriteEndElement();
textWriter.WriteStartElement("address", "");
textWriter.WriteString(pb.address);
textWriter.WriteEndElement();
textWriter.WriteStartElement("email", "");
textWriter.WriteString(pb.email);
textWriter.WriteEndElement();
//写入文档结束,调用WriteEndDocument方法
textWriter.WriteEndDocument();
//关闭textWriter
textWriter.Close();
return "true";
}
catch (Exception ex)
{
return ex.ToString();
}
}
补充:.NET技术 , ASP.NET