正在搜索 xml 数据网格视图

Searching xml 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

这就是我添加一些数据并单击保存时的样子

和table

你没有做的一件事是用你的 xml 做某事。我使用了你的基本代码,只是稍微修改了一下。

试试这个,它会让你开始,把你的 xml 流式传输到 class 对象中。

1.) 创建对象
项目XML数据

项目XML数据产品

如果您有 Visual Studio 2012 并且您的项目已打开。复制您的 xml 文件,然后进入项目中的 Class 并转到编辑,然后选择性粘贴并粘贴 XML 为 Class 这将创建您的 class对象。项目XML数据和项目XML数据产品

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

这些是您的对象 Classes 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