如何在变量中存储Listview中的数据? [英] How Do I Store Data From A Listview In A Variable?

查看:76
本文介绍了如何在变量中存储Listview中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为刚刚完成的程序创建一个save方法,并且需要保存的大多数值都很简单,可以保存在txt中。 ItemBagList列表视图是个例外。我尝试过的每一种方法都是相同的,我需要将listview中的一个或多个项目存储在一个变量或数组中。



我用的最简单的方法发现是将项目存储在数据库表中,程序将数据存储在db表中,同时将其他所有内容保存到txt,然后当它打开txt时,它还将从db表重新填充listview。



这是我目前的代码:



  Dim  DbConnection  As   OleDbConnection(strConnectionString)
Dim SqlQry 作为 OleDbCommand( INSERT INTO Inventory([Item Name],[Item Quality],[Sell Price])VALUES @ItemName,@ ItemQuality,@ SellPrice,DbConnection)
< span class =code-keyword> Dim DeleteCmd As OleDbCommand( DELETE * FROM Inventory,DbConnection)
Dim ItemName 作为 字符串
Dim ItemPrice As 整数
Dim ItemQuality As String

DbConnection.Open()

DeleteCmd.CommandType = CommandType.Text

DeleteCmd.Connection = DbConnection

DeleteCmd.ExecuteNonQuery()

DbConnection.Close( )

SqlQry.Parameters.AddWithValue( @ ItemName,ItemName)
SqlQry.Parameters.AddWithV alue( @ ItemQuality,ItemQuality)
SqlQry.Parameters.AddWithValue( @ SellPrice,ItemPrice)

对于 每个 ItemBagList.Items

下一页





列表视图有3列:项目名称,项目质量和销售价格。我需要的是将适当的变量设置为等于适当的列并将它们插入到Inventory表中。任何人都可以帮助我吗?

解决方案

 对于 每个 lvi 作为 ListViewItem  ItemBagList.Items 
< span class =code-comment>' 在此创建命令并传递lvi.SubItems(0),lvi.SubItems(1)和lvi.SubItems(2)
' 作为参数
' 例如SqlQry.Parameters.AddWithValue(@ ItemName,lvi.SubItems(0))
下一步


I am trying to create a save method for the program I have just about finished, and most of the values that need to be to be saved were straightforward and could be saved in a txt. The exception is the ItemBagList listview. Every method I have tried has come to the same thing, I need to store one or more items from the listview in a variable or array.

The simplest method I have found is to store the items in a database table, the program would store the data in the db table at the same time it saves everything else to a txt, then when it opens the txt it will also repopulate the listview from the db table.

Here is my code currently:

Dim DbConnection As New OleDbConnection(strConnectionString)
Dim SqlQry As New OleDbCommand("INSERT INTO Inventory ([Item Name], [Item Quality], [Sell Price]) VALUES @ItemName, @ItemQuality, @SellPrice", DbConnection)
Dim DeleteCmd As New OleDbCommand("DELETE * FROM Inventory", DbConnection)
Dim ItemName As String
Dim ItemPrice As Integer
Dim ItemQuality As String

DbConnection.Open()

DeleteCmd.CommandType = CommandType.Text

DeleteCmd.Connection = DbConnection

DeleteCmd.ExecuteNonQuery()

DbConnection.Close()

SqlQry.Parameters.AddWithValue("@ItemName", ItemName)
SqlQry.Parameters.AddWithValue("@ItemQuality", ItemQuality)
SqlQry.Parameters.AddWithValue("@SellPrice", ItemPrice)

For Each item In ItemBagList.Items

Next



The listview has 3 columns: Item Name, Item Quality, and Sell Price. What I need is to set a the appropriate variable to be equal to the appropriate column and to insert them into the Inventory table. Can anybody help me?

解决方案

For Each lvi  As ListViewItem In ItemBagList.Items
' Create your command here and pass lvi.SubItems(0),lvi.SubItems(1) and lvi.SubItems(2)
' as the parameters
' For example SqlQry.Parameters.AddWithValue("@ItemName", lvi.SubItems(0))
Next


这篇关于如何在变量中存储Listview中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆