Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Updating Listbox Selected Values 1

Status
Not open for further replies.

OrWolf

MIS
Mar 19, 2001
291
0
0
I've tried searching for this one and I'm not finding the answer I'm looking for. I have a form that has a list box that shows employees. I need to know how to allow a user to select multiple employees in that list and save them in a related table as independant records.

For example the form is based on table Demo. When employees are selected for Demo 'ABC' they are written to the DemoEmployees table with a foreign key of 'ABC' so the next time the form is opened those same employees are selected in the listbox.

Does anyone have any good examples of this in action?
 
This assumes your table contains a column named ForeignKey to hold the "ABC", etc. If further assumes that you have populated the table before running the following code. Good Luck!
Code:
Private m_DemoTable As New DataTable 

Private m_FOREIGN_KEY_VALUE As String = "ABC"
Private m_FOREIGN_KEY_COLUMN As String = "ForeignKey"

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    ' Populate m_DemoTable so that it is available here.

    Me.ListBox1.DataSource = m_DemoTable.DefaultView
    Me.ListBox1.ClearSelected()
    For index As Integer = 0 To m_DemoTable.Rows.Count - 1
        If m_DemoTable.Rows(index)(m_FOREIGN_KEY_COLUMN) = _
            m_FOREIGN_KEY_VALUE Then
                ' Set value.
                Me.ListBox1.SetSelected(index, True)
                ' Clear it for next iteration.
                m_DemoTable.Rows(index)(m_FOREIGN_KEY_COLUMN) = String.Empty
        End If
    Next
End Sub 'Form1_Load

Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
    For Each rowIndex As Integer In Me.ListBox1.SelectedIndices
        m_DemoTable.Rows(rowIndex)(m_FOREIGN_KEY_COLUMN) = m_FOREIGN_KEY_VALUE
    Next
End Sub 'Form1_Closed

 
The initial code makes sense, however I need code to add records to the child table for each selected value to store it, or delete records no longer selected. I have methods already for adding and deleting, so I'm just hoping for help on cycling through and checking values.

Thanks!
 
Hello,

Sorry, misunderstood the question. In your form load event, read your child table values into an array list something like this:
Code:
Dim list As New ArrayList
For Each row As DataRow In YourChildTable.Rows
    list.Add(row("EmployeeName")
Next
Me.ListBox1.ClearSelected()
For index As Integer = 0 To YourParentTable.Rows.Count - 1
    If list.Contains(YourParentTable.Rows(index)("EmployeeName") Then
        Me.ListBox1.SetSelected(index, True)
    End If
Next
Then in the form closed code reverse the process:
Code:
YourChildTable.Clear
For index As Inteter = 0 To Me.ListBox1.SelectedIndices.Count - 1
    ' Create and populate new child table row.
    Dim row As DataRow = YourChildTable.NewRow
    row("ForeignKey") = "ABC"
    row("EmployeeName") = YourParentTable.Rows(index)("EmployeeName")
    YourChildTable.Rows.Add(row)
Next
Good Luck!
 
We're getting closer, but one of the key sticking point for me is grabbing the data from the list box. So if record two in the list box is selected how do I grab that employee name so I can assign it to the new record that I'm inserting? I tried using me.ListBox1.Items(index).Value but it said that Public member 'value' on type 'DataRowView' is not found.
 
I believe that I was able to figure it out. Thank you for the help!
 
Hello,

Just in case you still have questions, the SelectedIndices return an array of row indexes into the underlying DataSource. I am assuming that it is a DataTable. So you could do something like this:
Code:
Dim parentRow As DataRow = YourParentTable.Rows(index)
Dim row As DataRow = YourChildTable.NewRow
Once you have a DataRow, then the individual columns can be retrieved by parentRow("ColumnName") or parentRow.Item("ColumnName") something like this:
Code:
row("EmployeeName") = parentRow("EmployeeName")
Good Luck!
 
Everything is working now except it's not selected more than one stored value in the list when re-opening the form. It appears to be only selecting the last value found. Using debug I was able to see that it sets the select for each found value. Here is the code (Note that it's the final code so names have changed with PresentationPresenter being the child table and lstRequestedResource being ListBox1):

Me.lstRequestedResource.ClearSelected()
For Index As Integer = 0 To Me.PresentationData.PresentationPresenter.Rows.Count - 1
For lstRow As Integer = 0 To Me.lstRequestedResource.Items.Count - 1
If Me.PresentationData.PresentationPresenter.Rows(Index)("PresenterUserID").ToString = Me.lstRequestedResource.Items(lstRow)("ValueListEntryID").ToString Then
Me.lstRequestedResource.SetSelected((lstRow + 1), True)
End If
Next
Next

Thank you,
 
It's set to Multisimple and I'm able to select and save multiple values.
 
Try this and Good Luck!
Code:
Dim row As DataRow

' Build comma delimited list of values.
Dim values As String = ","
For Each row In PresentationPresenter.Rows
    values &= row("PresenterUserID").ToString & ","
Next

With Me.lstRequestedResource
    .ClearSelected()
    ' Assuming your DataSource is a DataTable.
    Dim table As DataTable = DirectCast(.DataSource, DataTable)
    'Dim table As DataTable = DirectCast(.DataSource, DataView).Table

    For index As Integer = 0 To table.Rows.Count - 1
        row = table.Rows(index)
        If values.IndexOf(row("ValueListEntryID").ToString) > -1 Then
            .SetSelected(index, True)
        End If
    Next
End With
 
Thanks for all the help on this one. I feels like it's so close. Here is the code that I have now. Note that using the table declaration with DataView it returns 20 rows with an error that 10 is an invalid index. Using the PresentationPresenter it returns 2 but doesn't highlight the correct values. The problem with the the 2 is that it doesn't seem to compare the ValueListEntryID to PresenterUserID.


'Build a comma delimited list of value
Dim row As DataRow
Dim values As String = ","
For Each row In Me.PresentationData.PresentationPresenter.Rows
values &= row("PresenterUserID").ToString & ","
Next

With Me.lstRequestedResource
.ClearSelected()
Dim table As DataTable = DirectCast(.DataSource, DataView).Table
'Dim table As DataTable = Me.PresentationData.PresentationPresenter

For index As Integer = 0 To table.Rows.Count - 1
row = table.Rows(index)
If values.IndexOf(row("PresenterUserID").ToString) > -1 Then
.SetSelected(index, True)
End If
Next
End With
 
Hello,

Is the comma delimited list of values building correctly? Is there a chance any of the rows have been deleted? If so, you'll need to check the RowState property something like this:
Code:
If row.RowState <> SomethingOrOtherDeleted Then
    ' The rest of your code.
End If
Also, if your DataSource is a DataView then do this:
Code:
Dim view As DataView = DirectCast(.DataSource, DataView)

For index As Integer = 0 To view.Count - 1
    row = view.item(index).Row
    ' The rest of your code as before.
Next
 
That worked find four values (the count of PresentationPresenter and those running SetSelected), however it still only shows one selected when the form opens (the first one).

 
In your form's load code, put a break point and check the value of Me.lstSelectedResource.SelectionMode. It almost sounds like it is set to One or None. Also, add the following after this line:
Code:
.SetSelected(index, True)
Debug.WriteLine(index.ToString & " True")
Check your Output to ensure that it is being called as you think it should. Good Luck!
 
The selection mode is multiextended which I added to my code and the output appears as:

0 True
1 True
2 True
3 True

On thing I just realized is that I placed the code in InitializeUI and I'm wondering if there is a better location for it.
 
For some reason this started working today, perhaps a good old restart was required. The final step in my process is removing records, which I have the logic for, but I can't seem to figure out how to get the value for x. So if I have rows 1, 3, and 4 selected, I need to know the GUID for row 2. I can use "me.lstResourceRequested.GetSelected(index)" to find if a row is selected, but I want something similar to retrieve the value of a row.
 
I'm not sure exactly what you are trying to do with your last step, rebuild the selected table? Something else?
 
The way it's built I can add new rows by determining which are part of my dataset. However I need a way to find which ones are un-selected so I can remove them.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top