DataGridView populates only first row
-
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
-
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
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[^]
-
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[^]
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?
-
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?
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[^]