当图像单击vb.net表单的ListView时,如何从sql表中的多列中检索值?

时间:2021-09-18 13:26:51

your valuable suggestions are highly appreciated.

您的宝贵建议非常感谢。

I created sql table as follows.

我创建了sql表如下。

Create table Products (
ItemCode int NOT NUll Primary Key,
ItemName varchar(255),
ItemImage varbinary(MAX),
Category int,
Price Money,
Note varchar(255))

Then I created a vb.net windows form which contains some textboxes and Listview. and gave following code at form load event.

然后我创建了一个vb.net窗体,其中包含一些文本框和Listview。并在表单加载事件中提供以下代码。

Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.Configuration.ConfigurationManager
Imports System.IO

Public Class items
    Dim sqlCon As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("myPosConString").ConnectionString)
    Dim Command As SqlCommand
    Dim Reader As SqlDataReader
    Dim i As Integer
    Dim Query As String


    'Fill Items with pictures into listview
    Private Sub FillItemListView()
        Dim Adapter As New SqlDataAdapter
        Dim dt_Images As New DataTable

        Try
            lvItem.Clear()
            Dim imglist As New ImageList
            imglist.ColorDepth = ColorDepth.Depth32Bit
            lvItem.LargeImageList = imglist
            lvItem.LargeImageList.ImageSize = New System.Drawing.Size(100, 100)
            sqlCon.Open()
            Query = "select * from products "
            Command = New SqlCommand(Query, sqlCon)
            Adapter.SelectCommand = Command
            Adapter.Fill(dt_Images)

            For Each dr As DataRow In dt_Images.Rows

                Dim img_buffer = CType(dr("ItemImage"), Byte())
                Dim img_stream As New MemoryStream(img_buffer, True)
                img_stream.Write(img_buffer, 0, img_buffer.Length)
                imglist.Images.Add(dr("ItemCode").ToString(), New Bitmap(img_stream))
                img_stream.Close()
                Dim lstv As New ListViewItem
                lstv.Text = dr("ItemName").ToString
                lstv.ImageKey = dr("ItemCode").ToString
                lvItem.Items.Add(lstv)
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub items_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        FillItemListView()

    End Sub

End Class

Everything is working as I wanted. But I want all my textboxes to get values from other columns of sql table when I click on a large image in Listview.

一切都按我的意愿运作。但是当我点击Listview中的大图像时,我希望我的所有文本框都能从sql表的其他列中获取值。

listview name is lvItem and txtboxes are txtCode, txtName, txtCategory, txtPrice, txtNote.

listview名称是lvItem,txtboxes是txtCode,txtName,txtCategory,txtPrice,txtNote。

Can you please teach me how to do this. thank you.

你能告诉我怎么做吗?谢谢。

1 个解决方案

#1


0  

If you need to use a View other than Details in the ListView then a ListView it must be, as the DataGridView doesn't support the other views.

如果你需要在ListView中使用View不是Details,那么它必须是ListView,因为DataGridView不支持其他视图。

I would suggest that you bind your DataTable to a BindingSource and then bind that to your TextBoxes. When the user selects an item in the ListView, you can assign the Index of that item to the Position of the BindingSource. That will then display the fields of the row at that index in the bound TextBoxes.

我建议您将DataTable绑定到BindingSource,然后将其绑定到TextBoxes。当用户选择ListView中的项目时,您可以将该项目的索引分配给BindingSource的位置。然后,它将在绑定的TextBoxes中显示该索引处的行的字段。

If the user can sort your ListView then you could also set the Sort property of the BindingSource to keep them in sync.

如果用户可以对ListView进行排序,那么您还可以设置BindingSource的Sort属性以使它们保持同步。

#1


0  

If you need to use a View other than Details in the ListView then a ListView it must be, as the DataGridView doesn't support the other views.

如果你需要在ListView中使用View不是Details,那么它必须是ListView,因为DataGridView不支持其他视图。

I would suggest that you bind your DataTable to a BindingSource and then bind that to your TextBoxes. When the user selects an item in the ListView, you can assign the Index of that item to the Position of the BindingSource. That will then display the fields of the row at that index in the bound TextBoxes.

我建议您将DataTable绑定到BindingSource,然后将其绑定到TextBoxes。当用户选择ListView中的项目时,您可以将该项目的索引分配给BindingSource的位置。然后,它将在绑定的TextBoxes中显示该索引处的行的字段。

If the user can sort your ListView then you could also set the Sort property of the BindingSource to keep them in sync.

如果用户可以对ListView进行排序,那么您还可以设置BindingSource的Sort属性以使它们保持同步。