[英]DataGridView does not rebuild xml file contents in vb.net
我正在嘗試從DataGridView中的xml文件重建表值。 首先,我閱讀xml文檔,然后將其作為數據源加載到DataGridView中
Try
Dim ds As DataSet = New DataSet
ds.ReadXml("data.xml")
bs.DataSource = ds.Tables(0)
GridView.DataSource = bs
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
現在單擊一個按鈕,我想重建數據表,我嘗試了不同的方法,但是不起作用
OnButton_Click
bs.ResetBindings(False) ' Only blinks but does not work '
OnButton_Click
bs.Clear()
bs.DataSource = ds.Tables(0) ' Still does not work '
OnButton_Click
GridView.DataSource = Nothing
GridView.DataSource = bs
OnButton_Click
GridView.Refresh()
有人可以指出我錯了嗎
---------------------更新-------------------
xml文件是單獨生成和維護的,而不是通過在gridview本身中進行編輯
Private Sub addClient(ByRef hwid As String, ByRef dataID As String, ByRef dataValue As String)
Dim xmlDoc As New XmlDocument()
xmlDoc.Load(_datapath)
Dim rootNode As XmlNode = xmlDoc.SelectSingleNode("users")
Dim userNodes As XmlNodeList = rootNode.SelectNodes("user")
Dim userNode As XmlNode = rootNode.SelectSingleNode("user")
Dim Isuid As Boolean = False
For Each uNode As XmlNode In userNodes
If IsNothing(uNode.Attributes("uid")) Then
Dim idAttr As XmlAttribute = xmlDoc.CreateAttribute("uid")
idAttr.InnerText = hwid
uNode.Attributes.Append(idAttr)
Dim idNode As XmlNode = xmlDoc.CreateElement(dataID)
idNode.InnerText = dataValue
userNode.AppendChild(idNode)
Else
If uNode.Attributes("uid").Value = hwid Then
' Alter data '
Isuid = True
userNode = uNode
GoTo end_of_for
End If
End If
Next
end_of_for:
If Isuid = True Then
' Alter the data '
If IsNothing(userNode.SelectSingleNode(dataID)) Then
Dim idNode As XmlNode = xmlDoc.CreateElement(dataID)
idNode.InnerText = dataValue
userNode.AppendChild(idNode)
Else
Dim idNode As XmlNode = userNode.SelectSingleNode(dataID)
idNode.InnerText = dataValue
userNode.AppendChild(idNode)
End If
Else
' Create a new one '
If rootNode.SelectSingleNode("user").Attributes("uid").Value <> hwid Then
userNode = xmlDoc.CreateElement("user")
rootNode.AppendChild(userNode)
Dim idAttr As XmlAttribute = xmlDoc.CreateAttribute("uid")
idAttr.InnerText = hwid
userNode.Attributes.Append(idAttr)
Dim idNode As XmlNode = xmlDoc.CreateElement(dataID)
idNode.InnerText = dataValue
userNode.AppendChild(idNode)
End If
End If
do_exit:
xmlDoc.Save(_datapath)
End Sub
這是我剩下的代碼
Private _datapath As String = "data.xml"
Dim bs As New BindingSource
Dim ds As New DataSet
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Add user element also '
If Not File.Exists(_datapath) Then
Dim xmlDoc As New XmlDocument()
Dim docNode As XmlNode = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", Nothing)
xmlDoc.AppendChild(docNode)
Dim rootNode As XmlNode = xmlDoc.CreateElement("users")
xmlDoc.AppendChild(rootNode)
Dim userNode As XmlNode = xmlDoc.CreateElement("user")
rootNode.AppendChild(userNode)
xmlDoc.Save(_datapath)
End If
Try
ds.ReadXml(_datapath)
bs.DataSource = ds.Tables(0)
ClientView.DataSource = bs
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
bs.ResetBindings(False)
End Sub
Edit2 :因此,如果我再次加載文件(button1_Click),它仍在工作。 我注意到,如果xml架構與xml文件不匹配,則無法正常工作。 如果架構不正確,我將得到與您相同的結果。 網格中沒有數據
編輯 :如果在某些情況下不更改xml的結構\\模式,則在重新綁定之前檢查。 這對我有用。 在閱讀xml之前也需要閱讀架構。
public partial class Form1 : Form
{
DataSet ds1 = new DataSet();
BindingSource bs1 = new BindingSource();
public Form1()
{
InitializeComponent();
}
//Initial
private void button1_Click(object sender, EventArgs e)
{
DataTable dt1 = new DataTable();
ds1.ReadXmlSchema("XMLFile1Schema.xml");
ds1.ReadXml("XMLFile1.xml", XmlReadMode.ReadSchema);
bs1.DataSource = ds1.Tables[0];
dataGridView1.DataSource = bs1;
}
//Change content
private void button2_Click(object sender, EventArgs e)
{
DataTable dt2 = new DataTable();
dt2.Columns.Add("Column1");
dt2.Columns.Add("Column2");
dt2.Rows.Add("B11", "B12");
dt2.Rows.Add("B21", "B22");
dataGridView1.DataSource = dt2;
}
//Reset
private void button3_Click(object sender, EventArgs e)
{
//dataGridView1.DataSource = ds1.Tables[0];
dataGridView1.DataSource = bs1;
}
}
xml模式:
<?xml version="1.0" standalone="yes"?>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="row" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="row">
<xs:complexType>
<xs:sequence>
<xs:element name="col1" type="xs:string" minOccurs="1" />
<xs:element name="col2" type="xs:string" minOccurs="1" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
xml文件:
<?xml version="1.0" encoding="utf-8" ?>
<DocumentElement>
<row>
<col1>X11</col1>
<col2>X12</col2>
</row>
<row>
<col1>X21</col1>
<col2>X22</col2>
</row>
</DocumentElement>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.