日期:2008-07-26  浏览次数:20575 次

我们将显示如何将一个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=&q