如何将数据从一个listview行保存到sql数据库? [英] How can i save data from only one listview row to sql database?
本文介绍了如何将数据从一个listview行保存到sql数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好
我的表格中只有一个列表视图。
我想双击一行并保存从特定行到sql数据库表的记录。
欢迎任何帮助。
如果frUpdate = True然后
Dim iCount As Integer = 0
Dim iLoop As Integer = 0
Dim cmd As New SqlCommand()
Dim lvitem
cmd.Connection = clsMSSQL.con
cmd.Connection.Open()
iCount = listitems.Items.Count
如果不是listitems.Items.Count = 0则
Do Until iLoop = listitems.Items.Count
lvitem = listitems.Items.Item(iLoop)
使用lvitem
cmd.CommandText =插入项目(MatID,StaffID,Itemname,Unit,Quantity)VALUES_
& ; ('& lvitem.subitems(0).text&','& frmAddEdit.txtAA.Text&','& lvitem.subitems(1).text&', '& lvitem.subitems(2).text&','& lvitem.subitems(3).text&')
cmd.ExecuteNonQuery()
End With
iLoop = iLoop + 1
lvitem = Nothing
Loop
MsgBox(ΕπιτυχημένηΑποθήκευση!,MsgBoxStyle.Information,Πληροφορία)
fill_items()
结束如果
结束如果
结束子
使用此代码保存所有列表视图中的记录。我想只保存一行
解决方案
而不是循环浏览列表只需选择已双击的已选择项目,例如
私有 Sub ListView1_MouseDoubleClick( ByVal 发件人作为系统。对象, ByVal e As System.Windows.Forms.MouseEventArgs)句柄 ListView1.MouseDoubleClick
Dim s As ListView = sender ' 这使您可以访问ListView的所有智能感知
Dim l As ListViewItem = s.Items(s.SelectedIndices( 0 ))' 这是点击的项目
Dim egData As String = l.SubItems( 0 )。ToString()' 获取在此项目中的数据
' 使用egData执行某些操作
结束 Sub
顺便说一句,你所拥有的循环可能会更整洁......而不是
Dim iLoop As 整数 = 0
...
如果 不 listitem s.Items.Count = 0 然后
做直到iLoop = listitems.Items.Count
...做东西
iLoop = iLoop + 1
< span class =code-keyword>循环
结束 如果尝试
Dim iLoop As 整数 = 1
...
iLoop< listitems.Items.Count
...做东西
iLoop = iLoop + 1
循环跨度>
Hi guys
I have a form with only a listview in it.
I want to doubleclick to one row and save the records from the specific row to a sql database table.
Please any help is welcome.
If frUpdate = True Then Dim iCount As Integer = 0 Dim iLoop As Integer = 0 Dim cmd As New SqlCommand() Dim lvitem cmd.Connection = clsMSSQL.con cmd.Connection.Open() iCount = listitems.Items.Count If Not listitems.Items.Count = 0 Then Do Until iLoop = listitems.Items.Count lvitem = listitems.Items.Item(iLoop) With lvitem cmd.CommandText = "insert into Items(MatID,StaffID,Itemname,Unit,Quantity) VALUES " _ & "('" & lvitem.subitems(0).text & "','" & frmAddEdit.txtAA.Text & "','" & lvitem.subitems(1).text & "','" & lvitem.subitems(2).text & "','" & lvitem.subitems(3).text & "')" cmd.ExecuteNonQuery() End With iLoop = iLoop + 1 lvitem = Nothing Loop MsgBox("Επιτυχημένη Αποθήκευση!", MsgBoxStyle.Information, "Πληροφορία") fill_items() End If End If End Sub
with this code it save all the records from listview. I want to save only one row
解决方案
Instead of looping through your list just pick the "selected" item that has been double-clicked e.g.
Private Sub ListView1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDoubleClick Dim s As ListView = sender 'this gives you access to all of the intellisense for ListView Dim l As ListViewItem = s.Items(s.SelectedIndices(0)) 'This is the item clicked Dim egData As String = l.SubItems(0).ToString() 'Get at the data in this item 'do something with egData End Sub
As an aside the loop that you do have could be a little tidier ... instead of
Dim iLoop As Integer = 0 ... If Not listitems.Items.Count = 0 Then Do Until iLoop = listitems.Items.Count ... doing stuff iLoop = iLoop + 1 Loop End IfTry
Dim iLoop As Integer = 1 ... While iLoop < listitems.Items.Count ... doing stuff iLoop = iLoop + 1 Loop
这篇关于如何将数据从一个listview行保存到sql数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文