GridView操作Xml数据源(添加、删除、修改)
2009-08-11 21:56
639 查看
最近在做一个参数维护的功能,有52中部品,每种部品都对应一个参数表,如果用表的话就要建52张表,这不是客户所希望的,所以决定使用XML作为数据源存储信息,以下是实现的代码:
1、XML数据源的文件名为Table.xml,结构如下:
<Tables>
<SOP>
<PitchY>0.40</PitchY>
<T>0.40</T>
<B>0.40</B>
<WM>0.20</WM>
<WM1>0</WM1>
<WP>0.40</WP>
<WP1>0</WP1>
<R>0.05</R>
<LM>L-0.2</LM>
<WCM>0.18</WCM>
<WCP>0.37</WCP>
</SOP>
<SOP>
<PitchY>2.54</PitchY>
<T>0.70</T>
<B>0.80</B>
<WM>0.60</WM>
<WM1>1</WM1>
<WP>0.30</WP>
<WP1>1</WP1>
<R>0.05</R>
<LM>L</LM>
<WCM>0.50</WCM>
<WCP>0.80</WCP>
</SOP>
<DCHIP>
<BodySize>1005</BodySize>
<L>0.50</L>
<W>0.60</W>
<D>0.40</D>
<T>0.20</T>
<S>0.05</S>
<R>0.05</R>
<Wm>0.53</Wm>
<Lm>0.45</Lm>
</DCHIP>
<DCHIP>
<BodySize>1608</BodySize>
<L>0.70</L>
<W>0.90</W>
<D>0.80</D>
<T>0.30</T>
<S>0.05</S>
<R>0.05</R>
<Wm>0.85</Wm>
<Lm>0.60</Lm>
</DCHIP>
<DCHIP>
<BodySize>5750</BodySize>
<L>1.50</L>
<W>5.30</W>
<D>4.10</D>
<T>0.70</T>
<S>0.15</S>
<R>0.05</R>
<Wm>5.20</Wm>
<Lm>1.30</Lm>
</DCHIP>
</Tables>
2、具体代码
string xmlPath = "Calculation/Tables.xml";
//恢复GridView正常状态
private void ResetParameter()
{
gvParameter.EditIndex = -1;
LoadParameter(ddlShapeType.SelectedValue);
}
//加载XML并显示在GridView里
private void LoadParameter(string shapeType)
{
DataSet dsRule = new DataSet();
dsRule.ReadXml(xmlPath);
DataTable dtRule = dsRule.Tables[shapeType];
gvParameter.DataSource = dtRule;
gvParameter.DataBind();
}
//新增事件
protected void lnbAddNew_Click(object sender, EventArgs e)
{
if (ddlShapeType.SelectedValue.Length == 0)
{
lblMsg.Visible = true;
lblMsg.Text = "Warning: Please select shape type !";
return;
}
else if (gvParameter.Rows.Count == 0)
{
lblMsg.Visible = true;
lblMsg.Text = "Warning: [" + ddlShapeType.SelectedValue + "] table not exists !";
return;
}
DataSet ds = new DataSet();
ds.ReadXml(xmlPath);
DataTable dt = ds.Tables[ddlShapeType.SelectedValue];
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
ds.WriteXml(xmlPath); //将修改写入Table.xml
ResetParameter();
}
//编辑事件
protected void gvParameter_RowEditing(object sender, GridViewEditEventArgs e)
{
gvParameter.EditIndex = e.NewEditIndex;
LoadParameter(ddlShapeType.SelectedValue);
GridViewRow row = gvParameter.Rows[e.NewEditIndex];
//前面2列是Delete、Edit,故从第三列开始
for (int i = 2; i < row.Cells.Count; i++)
{
TextBox tb = (TextBox)row.Cells[i].Controls[0];
tb.Width = 50;
}
}
//更新事件
protected void gvParameter_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = gvParameter.Rows[e.RowIndex]; //获得当前行
int numCell = row.Cells.Count; //共几列单元格(包含Edit和Delete 2列)
int currentRow = row.DataItemIndex; //对应DataSet对应的行索引
DataSet ds = new DataSet();
ds.ReadXml(xmlPath);
DataRow dr;
dr = ds.Tables[ddlShapeType.SelectedValue].Rows[row.DataItemIndex]; //找到对应与DataSet行
string[] str = null; //此数组定义表的列名
switch (ddlShapeType.SelectedValue)
{
case "SOP":
{
str =new string[]{ "PitchY", "T", "B", "WM", "WM1", "WP", "WP1", "R", "LM", "WCM", "WCP" };
break;
}
case "DCHIP":
{
str = new string[] { "BodySize", "L", "W", "D", "T", "S", "R", "Wm", "Lm" };
break;
}
}
int j = 0;
//从第3列开始,前面有2列是Edit和Delete
for (int i = 2; i < numCell; i++)
{
string cText = ((TextBox)row.Cells[i].Controls[0]).Text;
dr[str[j]] = cText;
j++;
}
ds.WriteXml(xmlPath); //将修改写入Table.xml
ResetParameter();
}
//取消修改
protected void gvParameter_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
ResetParameter();
}
//删除事件
protected void gvParameter_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = gvParameter.Rows[e.RowIndex];
int curr = row.RowIndex;
DataSet ds = new DataSet();
ds.ReadXml(xmlPath);
DataRow dr = ds.Tables[ddlShapeType.SelectedValue].Rows[curr];
dr.Delete();
ds.WriteXml(xmlPath);
ResetParameter();
1、XML数据源的文件名为Table.xml,结构如下:
<Tables>
<SOP>
<PitchY>0.40</PitchY>
<T>0.40</T>
<B>0.40</B>
<WM>0.20</WM>
<WM1>0</WM1>
<WP>0.40</WP>
<WP1>0</WP1>
<R>0.05</R>
<LM>L-0.2</LM>
<WCM>0.18</WCM>
<WCP>0.37</WCP>
</SOP>
<SOP>
<PitchY>2.54</PitchY>
<T>0.70</T>
<B>0.80</B>
<WM>0.60</WM>
<WM1>1</WM1>
<WP>0.30</WP>
<WP1>1</WP1>
<R>0.05</R>
<LM>L</LM>
<WCM>0.50</WCM>
<WCP>0.80</WCP>
</SOP>
<DCHIP>
<BodySize>1005</BodySize>
<L>0.50</L>
<W>0.60</W>
<D>0.40</D>
<T>0.20</T>
<S>0.05</S>
<R>0.05</R>
<Wm>0.53</Wm>
<Lm>0.45</Lm>
</DCHIP>
<DCHIP>
<BodySize>1608</BodySize>
<L>0.70</L>
<W>0.90</W>
<D>0.80</D>
<T>0.30</T>
<S>0.05</S>
<R>0.05</R>
<Wm>0.85</Wm>
<Lm>0.60</Lm>
</DCHIP>
<DCHIP>
<BodySize>5750</BodySize>
<L>1.50</L>
<W>5.30</W>
<D>4.10</D>
<T>0.70</T>
<S>0.15</S>
<R>0.05</R>
<Wm>5.20</Wm>
<Lm>1.30</Lm>
</DCHIP>
</Tables>
2、具体代码
string xmlPath = "Calculation/Tables.xml";
//恢复GridView正常状态
private void ResetParameter()
{
gvParameter.EditIndex = -1;
LoadParameter(ddlShapeType.SelectedValue);
}
//加载XML并显示在GridView里
private void LoadParameter(string shapeType)
{
DataSet dsRule = new DataSet();
dsRule.ReadXml(xmlPath);
DataTable dtRule = dsRule.Tables[shapeType];
gvParameter.DataSource = dtRule;
gvParameter.DataBind();
}
//新增事件
protected void lnbAddNew_Click(object sender, EventArgs e)
{
if (ddlShapeType.SelectedValue.Length == 0)
{
lblMsg.Visible = true;
lblMsg.Text = "Warning: Please select shape type !";
return;
}
else if (gvParameter.Rows.Count == 0)
{
lblMsg.Visible = true;
lblMsg.Text = "Warning: [" + ddlShapeType.SelectedValue + "] table not exists !";
return;
}
DataSet ds = new DataSet();
ds.ReadXml(xmlPath);
DataTable dt = ds.Tables[ddlShapeType.SelectedValue];
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
ds.WriteXml(xmlPath); //将修改写入Table.xml
ResetParameter();
}
//编辑事件
protected void gvParameter_RowEditing(object sender, GridViewEditEventArgs e)
{
gvParameter.EditIndex = e.NewEditIndex;
LoadParameter(ddlShapeType.SelectedValue);
GridViewRow row = gvParameter.Rows[e.NewEditIndex];
//前面2列是Delete、Edit,故从第三列开始
for (int i = 2; i < row.Cells.Count; i++)
{
TextBox tb = (TextBox)row.Cells[i].Controls[0];
tb.Width = 50;
}
}
//更新事件
protected void gvParameter_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = gvParameter.Rows[e.RowIndex]; //获得当前行
int numCell = row.Cells.Count; //共几列单元格(包含Edit和Delete 2列)
int currentRow = row.DataItemIndex; //对应DataSet对应的行索引
DataSet ds = new DataSet();
ds.ReadXml(xmlPath);
DataRow dr;
dr = ds.Tables[ddlShapeType.SelectedValue].Rows[row.DataItemIndex]; //找到对应与DataSet行
string[] str = null; //此数组定义表的列名
switch (ddlShapeType.SelectedValue)
{
case "SOP":
{
str =new string[]{ "PitchY", "T", "B", "WM", "WM1", "WP", "WP1", "R", "LM", "WCM", "WCP" };
break;
}
case "DCHIP":
{
str = new string[] { "BodySize", "L", "W", "D", "T", "S", "R", "Wm", "Lm" };
break;
}
}
int j = 0;
//从第3列开始,前面有2列是Edit和Delete
for (int i = 2; i < numCell; i++)
{
string cText = ((TextBox)row.Cells[i].Controls[0]).Text;
dr[str[j]] = cText;
j++;
}
ds.WriteXml(xmlPath); //将修改写入Table.xml
ResetParameter();
}
//取消修改
protected void gvParameter_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
ResetParameter();
}
//删除事件
protected void gvParameter_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = gvParameter.Rows[e.RowIndex];
int curr = row.RowIndex;
DataSet ds = new DataSet();
ds.ReadXml(xmlPath);
DataRow dr = ds.Tables[ddlShapeType.SelectedValue].Rows[curr];
dr.Delete();
ds.WriteXml(xmlPath);
ResetParameter();
相关文章推荐
- GridView操作Xml数据源(添加、删除、修改)
- GridView操作Xml数据源(添加、删除、修改)
- XML文件操作(查询,添加,修改,删除 结点)
- C#中操作XML (节点添加,修改,删除完整版)
- 通过Dataset操作xml数据(添加,删除,修改)
- asp.net 操作xml(添加删除修改查询)
- java操作xml——添加、修改、删除、遍历
- 一个使用GridView显示数据,并且可以进行添加、修改、删除操作的例子
- XML文件操作(查询,添加,修改,删除 结点)
- .net 操作XML(1)添加,删除,修改,查询
- 这是一个关于XML文档的操作管理器XMLHelper类,类中包括XML文档的创建,文档节点和属性的读取,添加,修改,删除的方法功能的实现
- C# 操作xml(新建,添加,修改,删除)
- C# 生成 添加 修改 删除 XML节点操作
- delphi 注册表操作(读取、添加、删除、修改)完全手册
- html dom节点操作(获取/修改/添加或删除)
- html dom节点操作(获取/修改/添加或删除)
- Cookie添加,修改,删除等操作
- 06_XML的写入_dom4j添加、删除、修改Xml文件内容
- PHP MYSQL对数据库进行添加、修改、删除等一系列操作
- Python3列表元素的修改,添加( append(),inset() )和删除( del,pop(),remove() )操作