I may be missing something simple and obvious, but I can't figure it out.
I have a datagridviewlistbox with this code:
Thanks for your help.Code:For Each itm As DataRowView In FrmSettings.PlayerList.Items
'need to get itm row index here.
Next
Printable View
I may be missing something simple and obvious, but I can't figure it out.
I have a datagridviewlistbox with this code:
Thanks for your help.Code:For Each itm As DataRowView In FrmSettings.PlayerList.Items
'need to get itm row index here.
Next
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:
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.vb.net Code:
For i = 0 To FrmSettings.PlayerList.Items Dim item = DirectCast(FrmSettings.PlayerList.Items(i), DataRowView) 'Use item here. Next
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.
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.
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.
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
Thread moved from the 'Database Development' forum to the 'VB.Net' forum.