Results 1 to 7 of 7

Thread: [RESOLVED] selectedindex of datagridview listbox items

  1. #1

    Thread Starter
    Banned
    Join Date
    Apr 2018
    Location
    https://t.me/pump_upp
    Posts
    79

    Resolved [RESOLVED] selectedindex of datagridview listbox items

    I may be missing something simple and obvious, but I can't figure it out.
    I have a datagridviewlistbox with this code:
    Code:
                    For Each itm As DataRowView In FrmSettings.PlayerList.Items
                        'need to get itm row index here.
                    Next
    Thanks for your help.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: selectedindex of datagridview listbox items

    Firstly, this question has nothing to do with database development. The fact that the data came from a database is not relevant to the question, so I've asked the mods to move this thread to the VB.NET form.

    Secondly, what is a "datagridviewlistbox"? Do you mean a DataGridView control, a ListBox control or something else? The code you posted seems to simply be enumerating the items in a bound ListBox.

    As for the question, if the index matters to you then us a For loop instead of a For Each loop. In that case, the loop counter IS the index of the item and you use to get the item by index:
    vb.net Code:
    1. For i = 0 To FrmSettings.PlayerList.Items
    2.     Dim item = DirectCast(FrmSettings.PlayerList.Items(i), DataRowView)
    3.  
    4.     'Use item here.
    5. Next
    That said, do you really need that index? What exactly do you intend to use it for? Maybe you need it, maybe you actually don't.

  3. #3

    Thread Starter
    Banned
    Join Date
    Apr 2018
    Location
    https://t.me/pump_upp
    Posts
    79

    Re: selectedindex of datagridview listbox items

    Thanks Jim. The listbox is related to a datagridview and comes from the DataSource table options.
    Anyway, I figured out to do it a simular way to what you just showed. I need the index to the select the row in the corresponding datagridview.
    BTW, I am an untrained amateur and this is my first time dealing with any kind of Database.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: selectedindex of datagridview listbox items

    So you have a DataGridView and a ListBox. Are they bound to the same list? If so then you don't need to do anything to keep them in sync. You simply assign the same BindingSource to the DataSource property of each one and then making a selection in one will be reflected in the other.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: selectedindex of datagridview listbox items

    Quote Originally Posted by Amerigoware View Post
    BTW, I am an untrained amateur and this is my first time dealing with any kind of Database.
    Like I said, the database isn't actually relevant, to this question at least. Your controls are bound to one or more DataTables, probably via a BindingSource but not necessarily. The fact that you are populating the DataTable from a database doesn't change anything about your presentation code. You could just create a DataTable and populate it manually with literal values and the way you used that DataTable in a data-binding scenario would not change.

  6. #6

    Thread Starter
    Banned
    Join Date
    Apr 2018
    Location
    https://t.me/pump_upp
    Posts
    79

    Re: selectedindex of datagridview listbox items

    What I was trying to do was way more complicated than necessary. I've got it fixed and working as expected now.
    I have a number of items in the listbox (assigned to the same datasource as the datagridview). Some of the items are saved to a listof string in My.Settings, because they're currently being used and the other's are not. I am randomly selecting one that is currently in use.
    Code:
        Public Sub SelectName()
            '***PICK NAME***
            If My.Settings.CurrentPlayers.Count > 0 Then
                pick = rand.Next(0, My.Settings.CurrentPlayers.Count)
                Dim Itm As DataRowView = FrmSettings.PlayerList.Items(pick)
                Dim listedPlayer As String = DirectCast(Itm, DataRowView).Row.Item("Name").ToString
                If My.Settings.CurrentPlayers.Contains(listedPlayer) Then
                    FrmSettings.TableDataGridView.Rows(pick).Selected = True 'This not really necessary
                    D_Name = FrmSettings.TableDataGridView.Rows(pick).Cells(1).Value.ToString
                    D_Age = GetAge(FrmSettings.TableDataGridView.Rows(pick).Cells(2).Value)
                    D_Gender = FrmSettings.TableDataGridView.Rows(pick).Cells(3).Value
                Else
                    SelectName()
                End If
            End If
        End Sub

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: selectedindex of datagridview listbox items

    Thread moved from the 'Database Development' forum to the 'VB.Net' forum.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width