搜索xml datagridview

时间:2015-01-13 20:51:51

标签: xml vb.net visual-studio-2010 datagridview

我试图使其成为搜索文本框中的用户类型,将搜索结果缩小到特定行。刷新按钮也应该显示所有行。如何让它同时搜索所有列和行?每列的数据类型是字符串。我故意隐藏主键列并使用XML来保持简单。

Public Class Form1

    Private xmlDatabaseData As String = My.Application.Info.DirectoryPath & "\xmlPriceData.xml"

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        If My.Computer.FileSystem.FileExists(xmlDatabaseData) = True Then
            ItemXMLData.ReadXml(xmlDatabaseData)

        End If
    End Sub

    Private Sub txbSearch_TextChanged(sender As System.Object, e As System.EventArgs) Handles txbSearch.TextChanged
        Me.ProductsBindingSource.ite = txbSearch.Text

    End Sub

    Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
        Me.Validate()
        ProductsBindingSource.EndEdit()
        ItemXMLData.WriteXml(xmlDatabaseData)
    End Sub

    Private Sub Form1_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        Me.Validate()
        ProductsBindingSource.EndEdit()
        ItemXMLData.WriteXml(xmlDatabaseData)
    End Sub

    Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click
        ProductsBindingSource.AddNew()
    End Sub

    Private Sub btnDelete_Click(sender As System.Object, e As System.EventArgs) Handles btnDelete.Click
        Select Case MsgBox("Are you sure you want to delete the selected item? ", MsgBoxStyle.YesNo, "Confirm")
            Case MsgBoxResult.Yes
                Try
                    Me.ProductsBindingSource.RemoveCurrent()
                    Me.Validate()
                    Me.ProductsBindingSource.EndEdit()
                    ItemXMLData.WriteXml(xmlDatabaseData)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            Case MsgBoxResult.No

            Case Else

        End Select
    End Sub

    Private Sub btnRefresh_Click(sender As System.Object, e As System.EventArgs) Handles btnRefresh.Click
        If My.Computer.FileSystem.FileExists(xmlDatabaseData) = True Then
            ItemXMLData.ReadXml(xmlDatabaseData)

        End If
    End Sub 
End Class

这是gui

enter image description here

当我添加一些数据并点击保存时,它的外观如何

enter image description here

和表格

enter image description here

1 个答案:

答案 0 :(得分:1)

你没有做的一件事就是用你的xml做点什么。我使用了你的基本代码,只是修改了一下。

试试这个它会让你开始并拿走你的xml并将它流式传输到类对象中。

1。)创建你的对象
ItemXMLData

ItemXMLDataProducts

如果您有Visual Studio 2012并且您的项目已打开。复制您的xml文件,然后进入项目中的一个Class,然后转到Edit,然后选择Paste Special和Paste XML as Class,这将创建您的类对象。 ItemXMLData和ItemXMLDataProducts

Imports System.Data.OleDb
Imports System.Xml.Serialization
Imports System.IO
Imports System.Xml
Public Class Form1


Private xmlDatabaseData As String = Directory.GetCurrentDirectory & "\xmlPriceData.xml"

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load


    If My.Computer.FileSystem.FileExists(xmlDatabaseData) = True Then
        Dim xmlString As String

        xmlString = File.ReadAllText(xmlDatabaseData).Trim
        Dim obj As New ItemXMLData
        obj = ConvertFromXml(xmlString, GetType(ItemXMLData), System.Text.Encoding.UTF8)


        DataGridView1.DataSource = obj.Products

    End If
End Sub


Public Shared Function ConvertFromXml(ByVal xml As String, ByVal objType As System.Type, ByVal encoding As System.Text.Encoding) As Object

    Dim o As Object = Nothing


    Dim serializer As XmlSerializer = New XmlSerializer(objType)
    Using ms As MemoryStream = New MemoryStream(encoding.GetBytes(xml))
        Using xr As XmlTextReader = New XmlTextReader(ms)
            o = serializer.Deserialize(xr)
        End Using
    End Using



    Return o
End Function
End Class

这些是您的对象类ItemXMLData& ItemXMLDataProducts

<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True), _
 System.Xml.Serialization.XmlRootAttribute([Namespace]:="", IsNullable:=False)> _
Partial Public Class ItemXMLData

Private productsField() As ItemXMLDataProducts

<System.Xml.Serialization.XmlElementAttribute("Products")> _
Public Property Products() As ItemXMLDataProducts()
    Get
        Return Me.productsField
    End Get
    Set(value As ItemXMLDataProducts())
        Me.productsField = Value
    End Set
End Property
End Class


<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)> _
Partial Public Class ItemXMLDataProducts

Private idField As Byte

Private barcodeField As String

Private nameField As String

Private priceField As Decimal


Public Property ID() As Byte
    Get
        Return Me.idField
    End Get
    Set(value As Byte)
        Me.idField = Value
    End Set
End Property


Public Property Barcode() As String
    Get
        Return Me.barcodeField
    End Get
    Set(value As String)
        Me.barcodeField = Value
    End Set
End Property


Public Property Name() As String
    Get
        Return Me.nameField
    End Get
    Set(value As String)
        Me.nameField = Value
    End Set
End Property


Public Property Price() As Decimal
    Get
        Return Me.priceField
    End Get
    Set(value As Decimal)
        Me.priceField = Value
    End Set
End Property
End Class