0

I'm trying to add values, located in a ListView, inside a DataGridView. The strange behaviour is that VS skips the adding process from the Listview to the DataGridView, so the DGV remains empty. I said that the compiler skips this process because the commands written after the code I'm reporting are done. I've also tried to replicate this section of the software on a new project and it works properly. In the listbox there are only numbers and I verified, through a debug windows, that the Listview is not empty. Here is the code I'm using:

For Each item As ListViewItem In Form25.ListView1.Items
    With item
        Dim elemCorrente() As String = { .Text, .SubItems(1).Text}
        Form2.DataGridView2.Rows.Add(elemCorrente)
    End With
Next

Where do am I doing wrong? Thanks all are gonna help me. Best regards

  • Where are you declaring the instance of your Form2. Your are setting the DataGridView on the default instance not the one you have created – Mark Hall Dec 15 '20 at 22:17
  • See this Question https://stackoverflow.com/questions/4698538/why-is-there-a-default-instance-of-every-form-in-vb-net-but-not-in-c – Mark Hall Dec 15 '20 at 22:25
  • @Mark Hall thanks a lot for your answer. I'm not an expert programmer, could you explain me what do you mean please? – telemaco10399 Dec 15 '20 at 22:26
  • usually you declare a form like this: `Dim frm2 = New Form2 `and then use `frm2.Show()` to make it visible . VB.Net makes a default instance when you use the Class name like you did, it is usually not the one you are looking at. Try adding a Form2.Show() after your `For Each` statement to see if you see what you are expecting. the problem could also be in Form15.ListView1.Items since you are also using the default instance for that form . – Mark Hall Dec 15 '20 at 22:33
  • When I Press the button, form 2 is opened yet and I can see that the DGV is empty. Do I have to put that string even if Form 2 is opened yet? – telemaco10399 Dec 15 '20 at 22:35
  • What is the code that you are using to open the form ? – Mark Hall Dec 15 '20 at 22:36
  • This Is the path: FORM2>FORM23>FORM29>FORM2 – telemaco10399 Dec 15 '20 at 22:38
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/226008/discussion-between-mark-hall-and-telemaco10399). – Mark Hall Dec 15 '20 at 22:39
  • This is almost certainly an issue related to default instances. Stop what you're doing and go and learn about default instances. It's not our job to teach you that here. You might start [here](http://jmcilhinney.blogspot.com/2009/07/vbnet-default-form-instances.html). – jmcilhinney Dec 16 '20 at 00:16

0 Answers0