搜索xml datagridview [英] Searching xml datagridview
问题描述
我试图使它成为搜索文本框中的用户类型,将搜索结果缩小到一个特定的行。另外刷新按钮应该显示所有行。如何让它同时搜索所有列和行?每列的数据类型是字符串。我有意使主键列隐藏并使用XML来保持简单。
公共类Form1
Private xmlDatabaseData As String = My.Application.Info.DirectoryPath& \xmlPriceData.xml
Private Sub Form1_Load(sender As System.Object,e As System.EventArgs)处理MyBase.Load
如果My.Computer.FileSystem.FileExists(xmlDatabaseData) = True然后
ItemXMLData.ReadXml(xmlDatabaseData)
如果
结束Sub
私有子txbSearch_TextChanged(sender As System.Object,e As System .EventArgs)处理txbSearch.TextChanged
Me.ProductsBindingSource.ite = txbSearch.Text
End Sub
Private Sub btnSave_Click(sender As System.Object,e As System.EventArgs)处理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)处理MyBase.FormClosing
Me.Validate()
ProductsBindingSource.EndEdit()
Ite mXMLData.WriteXml(xmlDatabaseData)
End Sub
Private Sub btnAdd_Click(sender As System.Object,e As System.EventArgs)处理btnAdd.Click
ProductsBindingSource.AddNew()
End Sub
Private Sub btnDelete_Click(sender As System.Object,e As System.EventArgs)处理btnDelete.Click
选择案例MsgBox(你确定要删除选定项目? ,MsgBoxStyle.YesNo,确认)
案例MsgBoxResult.Yes
尝试
Me.ProductsBindingSource.RemoveCurrent()
Me.Validate()
Me.ProductsBindingSource .EndEdit()
ItemXMLData.WriteXml(xmlDatabaseData)
Catch ex As Exception
MsgBox(ex.Message)
End尝试
案例MsgBoxResult.No
案例Else
结束选择
End Sub
私有子btnRefresh_Click(发件人作为System.Object,e As System.EventArgs)处理btnRefresh.Click
如果My.Computer.FileSystem.FileExists(xmlDatabaseData)= True然后
ItemXMLData.ReadXml(xmlDatabaseData)
如果
结束Sub
结束类
这是gui
当我添加一些数据并点击保存时,它的外观如何?
和表
你没有做的一件事是用你的xml做一些事情。我使用你的基础代码,只是修改了一下。
尝试这样,它会让你开始,拿你的xml并将其流化成一个类对象。
1。)创建对象
ItemXMLData
ItemXMLDataProducts
如果您有Visual Studio 2012并且您的项目打开。复制您的xml文件,然后进入项目中的类,然后转到编辑,然后选择粘贴特殊并将XML粘贴到类中,这将创建您的类对象。 ItemXMLData和ItemXMLDataProducts
导入System.Data.OleDb
导入System.Xml.Serialization
导入系统。 IO
导入System.Xml
公共类Form1
私有xmlDatabaseData As String = Directory.GetCurrentDirectory& \xmlPriceData.xml
私有子Form1_Load(发件人As System.Object,e As System.EventArgs)处理MyBase.Load
如果我的。 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
公共共享函数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)
使用ms As MemoryStream = New MemoryStream(encoding.GetBytes(xml))
使用xr As XmlTextReader = New XmlTextReader(ms)
o = seria lizer.Deserialize(xr)
结束使用
结束使用
返回o
结束函数
结束类
这些是您的对象类ItemXMLData& ItemXMLDataProducts
< System.Xml.Serialization.XmlTypeAttribute(AnonymousType:= True),_
System.Xml。 Serialization.XmlRootAttribute([Namespace]:=,IsNullable:= False)> _
部分公共类ItemXMLData
私有productsField()作为ItemXMLDataProducts
< System.Xml.Serialization.XmlElementAttribute(Products)> _
公共财产产品()作为ItemXMLDataProducts()
获取
返回Me.productsField
结束获取
设置(作为ItemXMLDataProducts())
我.productsField = Value
结束集
结束属性
结束类
< System.Xml.Serialization.XmlTypeAttribute(AnonymousType:= True)> ; _
部分公共类ItemXMLDataProducts
私人idField作为字节
私人条形码字段作为字符串
私有名称字段作为字符串
私人价格区域作为十进制
公共属性ID()作为字节
获取
返回Me.idField
结束获取
设置(值As Byte)
Me.idField =值
结束集
结束属性
公共属性Barcode()As String
获取
返回Me.barcodeField
结束获取
设置(值As String)
Me.barcodeField =值
结束集
结束属性
公共属性名称()As String
获取
返回Me.nameField
结束Get
Set(value As String)
Me.nameField =值
结束集
结束属性
公共财产价格()作为十进制
获取
返回Me.priceField
结束Get
Set(value As Decimal)
Me.priceField = Value
结束集
结束属性
结束类
I'm trying to make it so that as user types in the search textbox it narrows down search result to a specific row. Also refresh button should show all rows. How do I make it search all columns and rows at the same time? datatype for each column is string. I intentionally made the primary key column hidden and using XML to keep it simple.
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
Here is the gui
That's how it looks when i add some data and click save
and the table
One thing that you are not doing is doing something with your xml. I used your base code and just modified it a bit.
Try this it will get you started and take your xml and stream it into an class objects.
1.) create your objects
ItemXMLData
ItemXMLDataProducts
If you have Visual Studio 2012 and your project open. Copy your xml file and then go into a Class within your project and go to Edit then Paste Special and Paste XML as Class this will create your class objects. ItemXMLData and 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
These are your object 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
这篇关于搜索xml datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!