【问题标题】:Searching xml datagridview搜索 xml datagridview
【发布时间】:2015-01-13 20:51:51
【问题描述】:

我正在尝试这样做,以便当用户在搜索文本框中键入时,它会将搜索结果缩小到特定行。刷新按钮也应该显示所有行。如何让它同时搜索所有列和行?每列的数据类型是字符串。我故意隐藏了主键列并使用 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

这是界面

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

还有桌子

【问题讨论】:

  • 您发布的代码对 XML 没有任何作用。你读写ItemXMLData,但你永远不会改变它。改变它的代码在哪里?
  • 也许你最好告诉我们ItemXMLData是如何定义的。
  • 感谢您指出标题。当我调试它通过单击添加按钮添加行。我可以在单元格上写任何东西点击保存并保存它。如果我在特定单元格上更改它并单击保存它就可以完成这项工作。我以为我不需要额外的代码来更改它们。我的目标是将 xml 文件作为数据源,这样如果程序要安装在另一台机器上,我就可以复制和粘贴包含所有数据的单个 xml 文件。这样我就不必重新输入所有记录。如果这不是一个好方法,请提出建议。

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


【解决方案1】:

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

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

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

ItemXMLDataProducts

如果您有 Visual Studio 2012 并且您的项目已打开。复制您的 xml 文件,然后进入项目中的一个类,然后转到编辑,然后选择性粘贴并将 XML 粘贴为类,这将创建您的类对象。 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

【讨论】:

    猜你喜欢
    • 2010-11-09
    • 1970-01-01
    • 2012-01-09
    • 2011-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    相关资源
    最近更新 更多