c#

位置:IT落伍者 >> c# >> 浏览文章

使用c#(datagrid控件)编辑xml文件


发布日期:2020年03月11日
 
使用c#(datagrid控件)编辑xml文件

对xml文件的记录进行删除修改或增加新记录

利用了datagrid控件的sortcommand事件对xml里的记录进行排序

email:

<%@page language=c# Trace=true%>

<%@import namespace=SystemData%>

<%@import namespace=SystemIO%>

<script language=c# runat=server>

string xmlfile=booksxmlxpath;

void page_load(Object objEventArgs e)

{

xpath=ServerMapPath(xmlfile);

if(!PageIsPostBack)

{

Dataload(isbn);

}

}

void Dataload(string psort)

{

DataSet ds=new DataSet();

FileStream fs=new FileStream(xpathFileModeOpen);

dsReadXml(fs);

if(dsTablesCount==)

{

ResponseWrite(xml文件内无记录!!!!);

fsClose();

ResponseEnd();

}

TraceWarn(表记录数ConvertToString(dsTables[]RowsCount));

DataRow dr=dsTables[]NewRow();//新建一行

dr[ISBN] = Add ISBN;

dsTables[]RowsInsertAt(dr);//插入到第行位置

TraceWarn(表数目ConvertToString(dsTablesCount));//以红字显示调试信息

//gridDataSource=dsTables[]DefaultView;

//gridDataBind();

DataView dv=new DataView(dsTables[]);

TraceWarn(字串长度:+psortConvertToString(psortLength));//排序字符串的长度

if(psortLength>)

dvSort=psort;

gridDataSource=dv;

gridDataBind();

fsClose();

}

void grid_sort(Object objDataGridSortCommandEventArgs e)

{

if(gridEditItemIndex==)

Dataload(eSortExpression);

else

ResponseWrite(正在编辑暂不能排序!!);

}

void grid_edit(Object objDataGridCommandEventArgs e)

{

gridEditItemIndex=(int)eItemItemIndex;

show_del(hide);

Dataload();

}

void grid_cancel(Object objDataGridCommandEventArgs e)

{

gridEditItemIndex=;

show_del(show);

Dataload();

}

void grid_update(Object objDataGridCommandEventArgs e)

{

int numcell=eItemCellsCount;//单元格数目(eItem是当前发生事件的表格行)

int currentrow=eItemDataSetIndex;

//int curr=eItemItemIndex;//与上句等价可以不带(int)

TraceWarn(当前更新行号 = ConvertToString(currentrow));

//TraceWarn(当前更新行号 = ConvertToString(curr));

DataSet ds=new DataSet();

dsReadXml(xpath);//将xml模式和数据读取到dataSet;

DataRow dr;//表示DataTable中的一行信息

if(currentrow==)

dr=dsTables[]NewRow();

else

dr=dsTables[]Rows[eItemDataSetIndex ];

string[] str={isbn author title category comments};

int j=;

for(int i=;i<numcell;i++)//跳过column

{

j=j+;

string ctext;

ctext=((TextBox)eItemCells[i]Controls[])Text;

dr[str[j]] = ctext;

TraceWarn(ConvertToString(i)+str[j]+:每一行的文本ctext);

}

if(currentrow==)

{

ResponseWrite(加入新记录!!);

dsTables[]RowsInsertAt(dr);

}

dsWriteXml(xpath);//将表示dataset的xml写入到xml文件中包括数据和模式

gridEditItemIndex = ;//无此句仍在编辑界面

show_del(show);

Dataload();

}

void show_del(string state)

{

string tmp=state;

switch(tmp)

{

case show:

gridColumns[]Visible = true;

break;

case hide:

gridColumns[]Visible = false;

break;

default:

gridColumns[]Visible = true;

break;//也要带break

}

}

void initialize(Object objDataGridItemEventArgs e)//注意参数与其它函数不同

{

//eItemCells[]Text=aaaaa;//

if(eItemItemIndex==)//如果是第一行

{

LinkButton a=new LinkButton();

a=(LinkButton)eItemCells[]Controls[];

LinkButton a=new LinkButton();

a=(LinkButton)eItemCells[]Controls[];//在grid内建一个linkbutton控件

if(aText==删 除)

aText=;

if(aText==编 辑)

aText=[AddNew];

}

}

void grid_del(Object objDataGridCommandEventArgs e)

{

ResponseWrite(XX);

TraceWarn(正要删除ConvertToString(eItemItemIndex));//控件中的行数

int curr=eItemItemIndex;

DataSet ds=new DataSet();

dsReadXml(xpath);

DataRow dr=dsTables[]Rows[curr];//有一行是新加的

drDelete();//找到相应的数据行将其删除

dsWriteXml(xpath);

gridEditItemIndex = ;

Dataload();

}

</script>

<form runat=server>

<asp:datagrid id=grid runat=server

alternatingitemstylebackcolor=#eeeeee

headerstylebackcolor=lightyellow

fontsize=pt

allowsorting=true

onsortcommand=grid_sort

oneditcommand=grid_edit

oncancelcommand=grid_cancel

onupdatecommand=grid_update

onitemcreated=initialize

ondeletecommand=grid_del

bordercolor=#

>

<columns>

<asp:buttoncolumn text=删 除 commandname=delete/>

<asp:editcommandcolumn buttontype=linkbutton updatetext=更 新 canceltext=取 消 edittext=编 辑 headertext=/>

</columns>

</asp:datagrid>

</form>

xml文件(文件名:booksxml)

<?xml version= standalone=yes?>

<books>

<book>

<isbn>eewewe</isbn>

<author>fefdw</author>

<title>eeef</title>

<category>tg</category>

<comments>rrrgeqw</comments>

</book>

<book>

<isbn></isbn>

<author>ssdfdfe</author>

<title>fgregre</title>

<category>rertrt</category>

<comments>rrredqeq</comments>

</book>

<book>

<isbn></isbn>

<author>ssdfdfe</author>

<title>fgregre</title>

<category>rertrt</category>

<comments>rrredqeq</comments>

</book>

<book>

<isbn></isbn>

<author>Tom Peters</author>

<title>Circle of Innovation</title>

<category>marketing</category>

<comments>His most recent book is his best by far!</comments>

</book>

<book>

<isbn></isbn>

<author>Eli Goldthrait</author>

<title>The Goal</title>

<category>management</category>

<comments>Advocate of Theory of Constraints as applied to managment and optimization</comments>

</book>

<book>

<isbn>X</isbn>

<author>Jeff Cox Howard Stevens</author>

<title>Selling the Wheel</title>

<category>management</category>

<comments>Excellent Treatise/Novel on the entire Sales Cycle</comments>

</book>

<book>

<isbn></isbn>

<author>Alan Cooper</author>

<title>The Inmates Are Running The Asylum</title>

<category>management</category>

<comments>The father of Visual Basic and creator of the new art of Interaction Design very valuable in designing websites Basically the worlds most cutting edge thinker in User Interface design aimed at simplifying software use</comments>

</book>

</books>

               

上一篇:VS2005数据存取层深入剖析高级篇

下一篇:利用C#实现任务栏通知窗口