|
-
Apr 24th, 2018, 03:49 PM
#1
Thread Starter
Banned
[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.
-
Apr 24th, 2018, 08:57 PM
#2
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:
For i = 0 To FrmSettings.PlayerList.Items Dim item = DirectCast(FrmSettings.PlayerList.Items(i), DataRowView) 'Use item here. 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.
-
Apr 24th, 2018, 09:09 PM
#3
Thread Starter
Banned
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.
-
Apr 24th, 2018, 09:37 PM
#4
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.
-
Apr 24th, 2018, 09:40 PM
#5
Re: selectedindex of datagridview listbox items
 Originally Posted by Amerigoware
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.
-
Apr 24th, 2018, 09:48 PM
#6
Thread Starter
Banned
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
-
Apr 25th, 2018, 06:34 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|