日期:2013-09-10  浏览次数:20512 次

我们将显示如何将一个xml文件作为datagrid,这样对其的排序,编辑,等操作将是非常简单的事。
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<html>
<script language="VB" runat="server">

Sub Page_Load(Src As Object, E As EventArgs)
        If Not (IsPostBack)
            DataLoad("isbn")
        End If
End Sub

Sub DataLoad(parmsort as string)
         Dim ds As New DataSet
        Dim FS As New FileStream(Server.MapPath("books.xml"), FileMode.Open)
        ds.ReadXml(FS)
        MyDataGrid.DataSource = new DataView(ds.Tables(0))
        MyDataGrid.DataBind()
        FS.close()
END SUB

Sub DataSort(Src As Object, E As DataGridSortCommandEventArgs)
    ' Bug if we sort, then Edit Item Becomes Wrong
    IF MyDataGrid.EditItemIndex=-1 THEN
        DataLoad(e.sortexpression)
    ELSE
        response.write ("Can't sort until editing is done!")
    END IF
End Sub   

Sub DataDelete(Sender As Object, E As DataGridCommandEventArgs)
    DIM deletekey as string
    IF MyDataGrid.EditItemIndex=-1 THEN
        deletekey=MyDataGrid.DataKeys(CInt(E.Item.ItemIndex))
        response.write ("deleted " & deletekey)
    ELSE
        response.write ("Can't delete until editing is done!")
    END IF
END SUB


Sub DataEdit(Sender As Object, E As DataGridCommandEventArgs)
        DIM editkey as string
        MyDataGrid.EditItemIndex = Cint(E.Item.ItemIndex)
        editkey=MyDataGrid.DataKeys(CInt(E.Item.ItemIndex))
        'response.write ("To Be Edited" & editkey)
        DataLoad("")
End Sub

Sub DataCancel(Sender As Object, E As DataGridCommandEventArgs)
        MyDataGrid.EditItemIndex = -1
        response.write ("edit was cancelled")
        DataLoad("")
End Sub

Sub DataUpdate(Sender As Object, E As DataGridCommandEventArgs)
        DIM editkey as string
        MyDataGrid.EditItemIndex = -1
       editkey = MyDataGrid.DataKeys(CInt(E.Item.ItemIndex))
       response.write ("To Be Updated " & editkey)
        DataLoad("")
       ' howmanycols = E.Item.Cells.Count
End Sub

</script>

<body>

<h3><font face="Verdana">The Best Books Ever</font>
<span runat="server" id="MySpan"/></h3>

<form runat="server">
<ASP:DataG