Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. DataGridView populates only first row

DataGridView populates only first row

Scheduled Pinned Locked Moved Visual Basic
designhelpquestionannouncement
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sonhospa
    wrote on last edited by
    #1

    Hi, I hope you all had a merry Christmas and nice holidays with your beloved ones! :rose: I try to retrieve data from text files and show them in a Datagrieview control. Each of the files is represented in an instance of "SoundConfigFile", which are collected in a generic List(of SoundConfigFile). The problem is: Only one row (containing the data collected from the first file) shows up in the Datagridview!?! In the forms constructor, I set

        dgvList.AutoGenerateColumns = True
        dgvList.DataBindings.DefaultDataSourceUpdateMode = DataSourceUpdateMode.OnValidation
    

    . The longer task of extracting the required data from files is handled in a separate thread:

    Private Sub ThreadFunction()
    
        Try
            SoundConfigFiles = New List(Of SoundConfigFile)
            Dim dirs As String() = Directory.GetFiles(My.Settings.FSACDir, "sound.cfg", SearchOption.AllDirectories)
            Dim max As Integer = dirs.Count()
            Dim done As Integer = 0
            Dim percent As Integer = 0
            Dim msg As String = String.Empty
    
            For Each configfile As String In dirs
                SoundConfigFiles.Add(New SoundConfigFile(Me, configfile))
                done += 1
                percent = CInt(done / max \* 100)
                msg = String.Format("{0} finished ({1}%)", configfile, percent)
                'update the UI thread
                UpdateForm(configfile, percent, msg)
            Next
        Catch ex As Exception
        End Try
    End Sub 'ThreadFunction
    

    The UI thread is then updated in

    Public Sub UpdateForm(ByVal myItem As String, ByVal percent As Integer, ByVal msg As String)
    
        If Me.InvokeRequired Then
            Dim d As UpdateDelegate = AddressOf UpdateForm
            Me.Invoke(d, {myItem, percent, msg})
        Else
            Me.dgvList.DataSource = SoundConfigFiles
            Me.dgvList.Refresh()
            Me.ProgressBar1.Value = percent
            Me.myLabel.Text = msg
            Me.lblFinalMessage.Text = String.Format("{0} instances still open.", \_
                                           SoundConfigFile.instances)
    
        End If
    End Sub
    

    I tried to find relevant information for hours now, but I can't get it to work. Does anyone of you have a good advice or can tell me what I'm doing wrong here? :confused: Thank you, Mick

    L 1 Reply Last reply
    0
    • S Sonhospa

      Hi, I hope you all had a merry Christmas and nice holidays with your beloved ones! :rose: I try to retrieve data from text files and show them in a Datagrieview control. Each of the files is represented in an instance of "SoundConfigFile", which are collected in a generic List(of SoundConfigFile). The problem is: Only one row (containing the data collected from the first file) shows up in the Datagridview!?! In the forms constructor, I set

          dgvList.AutoGenerateColumns = True
          dgvList.DataBindings.DefaultDataSourceUpdateMode = DataSourceUpdateMode.OnValidation
      

      . The longer task of extracting the required data from files is handled in a separate thread:

      Private Sub ThreadFunction()
      
          Try
              SoundConfigFiles = New List(Of SoundConfigFile)
              Dim dirs As String() = Directory.GetFiles(My.Settings.FSACDir, "sound.cfg", SearchOption.AllDirectories)
              Dim max As Integer = dirs.Count()
              Dim done As Integer = 0
              Dim percent As Integer = 0
              Dim msg As String = String.Empty
      
              For Each configfile As String In dirs
                  SoundConfigFiles.Add(New SoundConfigFile(Me, configfile))
                  done += 1
                  percent = CInt(done / max \* 100)
                  msg = String.Format("{0} finished ({1}%)", configfile, percent)
                  'update the UI thread
                  UpdateForm(configfile, percent, msg)
              Next
          Catch ex As Exception
          End Try
      End Sub 'ThreadFunction
      

      The UI thread is then updated in

      Public Sub UpdateForm(ByVal myItem As String, ByVal percent As Integer, ByVal msg As String)
      
          If Me.InvokeRequired Then
              Dim d As UpdateDelegate = AddressOf UpdateForm
              Me.Invoke(d, {myItem, percent, msg})
          Else
              Me.dgvList.DataSource = SoundConfigFiles
              Me.dgvList.Refresh()
              Me.ProgressBar1.Value = percent
              Me.myLabel.Text = msg
              Me.lblFinalMessage.Text = String.Format("{0} instances still open.", \_
                                             SoundConfigFile.instances)
      
          End If
      End Sub
      

      I tried to find relevant information for hours now, but I can't get it to work. Does anyone of you have a good advice or can tell me what I'm doing wrong here? :confused: Thank you, Mick

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Let's see; we add item to a collection, set the datasource to the collection, get the next item. You stated that you only saw the first one. Try setting the datasource to "null" before assigning the collection :)

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

      S 1 Reply Last reply
      0
      • L Lost User

        Let's see; we add item to a collection, set the datasource to the collection, get the next item. You stated that you only saw the first one. Try setting the datasource to "null" before assigning the collection :)

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

        S Offline
        S Offline
        Sonhospa
        wrote on last edited by
        #3

        This works :-D Thank you, Eddy! :cool: But it's not enough to set the datasource to "Nothing" after startup (same result: only first row seen). I have do that (and afterwards assign the collection again) within the "UpdateUI" Sub. Doesn't that slow down the application massively? Is that a bug?

        L 1 Reply Last reply
        0
        • S Sonhospa

          This works :-D Thank you, Eddy! :cool: But it's not enough to set the datasource to "Nothing" after startup (same result: only first row seen). I have do that (and afterwards assign the collection again) within the "UpdateUI" Sub. Doesn't that slow down the application massively? Is that a bug?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Sonhospa wrote:

          Doesn't that slow down the application massively?

          You're right, it does. It'd be recommended not to set the datasource until the list is completely populated. If you want "smoother" loading using the DataGridView, you could implement a virtual mode[^] :)

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups