7 Replies - 419 Views - Last Post: 01 March 2018 - 07:56 AM Rate Topic: -----

#1 nosy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 10-March 14

Multiselect in ListBoxes

Posted 23 February 2018 - 03:05 PM

I have a list of student names in a listBox,(studentList)I click on a name in the box and get all the students details up ie name, course, subject etc.The code then gets the details from the database(in my case it's access) then displays it in a datagridview. The code works fine if I just select one item from one(or all)List Boxes.Is there a way I could multiselct(ie more than one) from each box. I know I can use SelectedMode property to allow the highlighting but that wont draw the required data from the database.Here is the code I am using vb.10
 `Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As  System.EventArgs) Handles Button1.Click
    Dim con As New OleDb.OleDbConnection 

    Dim dbProvider As String
    Dim dbSource As String

    Dim ds As New DataSet

    Dim tables As DataTableCollection = ds.Tables

    Dim source1 As New BindingSource()

    Dim da As New OleDb.OleDbDataAdapter        

    dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"

    dbSource = "Data Source = C:\Documents and Settings\Desktop  \studentmarks.accdb"
    con.ConnectionString = dbProvider & dbSource

    con.Open()

    Dim isFirstColumn As Boolean = True
    Dim student As String = ""
    Dim course As String = ""
    Dim grade As String = ""


    Dim x As String = studentList.Text
    Dim y As String = courseList.Text
    Dim z As String = gradeList.Text     


    Dim defaultSQL As String = "SELECT * FROM studentfile "       

    If studentList.SelectedIndex > -1 Then 
        If isFirstColumn Then
            student = "WHERE student = '" & x & "' " 
        Else
            student = "AND student = '" & x & "' "
        End If
        isFirstColumn = False
    End If

    If courseList.SelectedIndex > -1 Then 
        If isFirstColumn Then
            course = "WHERE course = '" & y & "' " 
        Else
            course = "AND course = '" & y & "' "
        End If
        isFirstColumn = False
    End If       

    If gradeList.SelectedIndex > -1 Then 
        If isFirstColumn Then
            grade = "WHERE grade = '" & z & "' " 
        Else
            grade = "AND grade = '" & z & "' "
        End If
        isFirstColumn = False
    End If       

    Dim sql As String = defaultSQL & student & course & grade

    da = New OleDb.OleDbDataAdapter(sql, con)

    da.Fill(ds, "topclass")


    Dim view1 As New DataView(tables(0))

    source1.DataSource = view1
    DataGridView1.DataSource = view1
    DataGridView1.Refresh()
    DataGridView1.DataSource = view1

    DataGridView1.Refresh()

    Dim cnt As Integer

    cnt = DataGridView1.Rows.Count

    TextBox1.Text = cnt - 1

    Dim dayclass As String = TextBox1.Text
    TextBox8.Text = dayclass
    con.Close()


End Sub` 


Is This A Good Question/Topic? 0
  • +

Replies To: Multiselect in ListBoxes

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14092
  • View blog
  • Posts: 56,461
  • Joined: 12-June 08

Re: Multiselect in ListBoxes

Posted 24 February 2018 - 10:57 AM

You would need to use a loop to iterated over the 'selected items' collection.. getting the data and adding it all to the single data set/data table.
Was This Post Helpful? 1
  • +
  • -

#3 nosy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 10-March 14

Re: Multiselect in ListBoxes

Posted 24 February 2018 - 03:02 PM

hi modi,
i thought that might be the case but where do these loops go?
regards
nosey
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14092
  • View blog
  • Posts: 56,461
  • Joined: 12-June 08

Re: Multiselect in ListBoxes

Posted 24 February 2018 - 03:05 PM

Presumably around/near your DB call.

Side note - you should be using parameters and not trying to splice your own WHERE statement. That's how the hackers get'cha.
Was This Post Helpful? 0
  • +
  • -

#5 nosy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 10-March 14

Re: Multiselect in ListBoxes

Posted 25 February 2018 - 08:01 AM

Hi Modi,
oops sorry in the bit where it says was this post helpful(which it certainly was)I have by mistake given you a -1 instead of a plus +1,not too sure how to undo it :stupid:
sorry about that is there a way to undo it
regards
Nosy
Was This Post Helpful? 0
  • +
  • -

#6 maceysoftware   User is offline

  • Foo
  • member icon

Reputation: 353
  • View blog
  • Posts: 1,533
  • Joined: 07-September 13

Re: Multiselect in ListBoxes

Posted 26 February 2018 - 04:32 AM

Rebalanced the helpful meter, so at least it's not a -1 anymore.
Was This Post Helpful? 0
  • +
  • -

#7 nosy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 10-March 14

Re: Multiselect in ListBoxes

Posted 01 March 2018 - 07:47 AM

hi all,
been messing about with thic for a few days but still cant get it to do what i want,any practical suggestions or examples of code would be much appreciated
regards nosy
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14092
  • View blog
  • Posts: 56,461
  • Joined: 12-June 08

Re: Multiselect in ListBoxes

Posted 01 March 2018 - 07:56 AM

What does your current code look like?

It's pretty straight forward.
CREATE dataset
START FOR to '# of list items selected'
   GET item from listbox at index
   SELECT data from DB based in item's id.
   MERGE returned info from the DB to the dataset
NEXT-FOR


Conversely you can get all the IDs and create a list of them and use them in a database SELECT statement with the list of numbers used with 'IN'.
SELECT *
FROM <db table name>
WHERE id in (1, 2, 3... etc)

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1