none
Populate a datagridview from values of listbox multiple items

    Question

  • hi
    I am a new to vb.net, need help plz
    I have a purchase order form having a datagridview and a button and another form having 2 listboxes and a button
    what I want to do is on click select companies button that other form of 2 listboxes shows.
     listbox2 is bound with a sql database column, when I double click on company name of listbox 2 it copies name into listbox 1
    but what I need is to populate datagridview on purchase order form with item datatable where all Companies like in listbox1

    Private Sub ListBox2_DoubleClick(sender AsObject, e AsEventArgs) HandlesListBox2.DoubleClick
    	If ListBox2.SelectedIndex < 0 Then
    		MsgBox("Please Select Data", MsgBoxStyle.Exclamation, "Form")
    	Else
    		If ListBox2.Items.Count > 0 Then
    			ListBox1.Items.Add(ListBox2.SelectedItem("Item_Name"))
    		End If
    	EndIf
    End Sub
    Private Sub Button3_Click(sender AsObject, e AsEventArgs) HandlesButton3.Click
    	Form4.Show()
    	fillorders()
    	Me.Close()
    End Sub
    Private Sub select_companies_po_Load(sender AsObject, e AsEventArgs) HandlesMyBase.Load
    	'TODO: This line of code loads data into the 'HaadiDataSet.Item' table. You can move, or remove it, as needed.
    	Me.ItemTableAdapter.Fill(Me.HaadiDataSet.Item)
    End Sub
    
    Sub fillorders()
    	cn.Open()
    	For i= 0 to ListBox1.Items.count - 1
    		qry = "SELECT * from Item where Item_Name LIKE '"& Convert.ToString(ListBox1.Items(i).Text) & "'"
            adp =NewSqlDataAdapter(qry, cn)
            ds = New DataSet
            adp.Fill(ds,"Item")
            Form4.DataGridView1.DataSource = ds
            Form4.DataGridView1.DataMember = ds.Tables("Item").ToString
    	Next
    	cn.Close()
    End Sub


    Tuesday, August 05, 2014 5:04 PM

Answers

  • Hi,

    Based on your description and specially the fillorders method you shared, I assume that you want to get the data from that database, and that data should has this option its Item_Name should like the items listed in ListBox1, right?

    If I misunderstood anything, please feel free to let me know.

    In this case, the key task is to generate the sql query, then fill the dataset which will be the datasource of that DataGridView. The way you shared will use different sql query to fill that datagridview multiple times, in this case, we just need one sql query.

    You could use the following way to generate that sql query.

     Dim qry As String = "SELECT * from Item "
            Dim lst As New List(Of String)
            For i = 0 To ListBox1.Items.Count - 1
                Dim st As String = ListBox1.Items(i).ToString()
                lst.Add(String.Format(" Item_Name LIKE '%{0}%' " _
                                      , ListBox1.Items(i).ToString()))
            Next
            If lst.Count > 0 Then
                qry += " where " +String.Join(" or ", lst.ToArray())
            End If

    Then use that query to get that data and fill the dataset to get that datagridview show the result.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, August 06, 2014 6:22 AM
    Moderator