2009-12-14 44 views
0

我有音樂播放列表信息,這些信息讀入到Visual Basic中的GridView控件現有的XML文檔。我現在想要將GridView中的所有更新保存到該xml文檔中。我怎樣才能做到這一點?如何在VB GridView控件更新保存到一個現有的XML文檔?

Private Sub cboUsers_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboUsers.SelectedIndexChanged 
    Dim songList As New XmlDocument 
    songList.LoadXml(s.GetPlaylist(cboUsers.SelectedItem.ToString())) 
    Grid.Rows.Clear() 
    Dim songs As XmlNodeList = songList.DocumentElement.SelectNodes("//Song") 
    Dim song As XmlNode 
    For Each song In songs 
     Dim artist As String = song.SelectSingleNode("artist").InnerText 
     Dim title As String = song.SelectSingleNode("title").InnerText 
     Dim length As String = song.SelectSingleNode("length").InnerText 
     Dim album As String = song.SelectSingleNode("album").InnerText 
     Dim popularity As String = song.SelectSingleNode("popularity").InnerText 
     Dim row() As Object = {artist, title, length, album, popularity} 
     Grid.Rows.Add(row) ' Add as a row in the Grid 
    Next 
End Sub 

感謝

回答

0

一種方法是添加自己的更新命令列

<asp:CommandField ShowEditButton="True" HeaderStyle-Width="80px" /> 

然後使用網格更新事件做你的工作。您可以從

GridViewUpdateEventArgs e 

String foo = e.NewValues["columnFoo"].ToString(); 

檢索新(老)值請您更新到XML,然後取消更新本身

e.Cancel = true; 

C#代碼示例,但很容易複製到VB。

編輯,添加VB代碼的要求

Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating 

    'Get some new values 
    Dim foo As String = e.NewValues("fooColumn").ToString() 

    'Im not sure how you plan to find the unique XML node so lets assume that your using title 

    'So either get it from a datakey, if your using them in the gridview 
    Dim titleFromDataKey As String = GridView1.DataKeys(e.RowIndex)("Title").ToString(); 

    'Or from the old values 
    Dim titleFromOldValues = e.OldValues("Title").ToString() 

    'Insert your logic to find the XML node and update it here 

    'Cancel the update 
    e.Cancel = True 

End Sub 
+0

我不熟悉C#,所以它不是那麼容易複製到VB對我來說...你能在VB提供一個例子嗎? – weedave 2009-12-14 13:19:41

+0

完成,希望足以與之合作? – Jammin 2009-12-14 14:46:23

相關問題