none
DataGridView - Loaded data not visible in column RRS feed

  • Question

  • Hi, I have a DataGridView that I load data into by reading a Textfile.  The import works perfectly, but when I view the DataGridView after the import, the first value does not show up until I left mouse click into the first cell. 

    eg:

    Before

    After

    I dont know, but it seems to resolve itself if I click into the new row at the bottom as well.  

    Any ideas appreciated

    Ian



    • Edited by KernowKiwi Wednesday, February 21, 2018 1:50 AM
    Wednesday, February 21, 2018 1:29 AM

Answers

  • Hi, 

    You seems not understand why I ask question (its real meaning), but anyway, I made a sample.





    This code is in [Import] button.
    You can simply move this into Form_Load event (MyBase.Load). 

    There are 3 pages in a TabControl.
    DataGridView "P2_DGV_1" is in 2nd page,
    DataGridView "P3_DGV_1" and "P3_DGV_2" are in 3rd page,
    ' ---[Import]
    Private Sub P2_btn_Import_Click(sender As Object, e As EventArgs) Handles P2_btn_Import.Click
    	Dim P2_DGV_1_RowsCnt As Integer = -1
    	Dim P3_DGV_1_RowsCnt As Integer = -1
    	Dim P3_DGV_2_RowsCnt As Integer = -1
    	' ---
    	Dim sPath As String = Me.myPath
    	Dim currentRow As String()  ' -- array for 1 Line/Row
    	Dim myCol As Integer = 0
    	' --- process all files in a folder
    	For Each sfile As String In Directory.GetFiles(sPath)
    		If (IO.Path.GetExtension(sfile).ToLower <> ".txt") Then
    			Continue For ' -- .txt 以外は対象外
    		End If
    		Using textFile As New FileIO.TextFieldParser(sfile)
    			With textFile
    				.TextFieldType = FileIO.FieldType.Delimited
    				.SetDelimiters("|")
    			End With
    			' --- Row-Loop: read textFile
    			While Not textFile.EndOfData
    				currentRow = textFile.ReadFields()  ' -- store 1 line to array 
    				' --- add Row
    				Select Case currentRow(0)
    					Case = "P2_DGV_1"
    						If (myCol = 0) Then
    							Me.P2_DGV_1.Rows.Add()
    							P2_DGV_1_RowsCnt += 1
    						End If
    						' --- Column-Loop: each column > P2_DGV_1
    						For Each currentData As String In currentRow
    							Me.P2_DGV_1(myCol, P2_DGV_1_RowsCnt).Value = currentData
    							myCol += 1
    						Next
    						' --- end of Column-Loop
    						myCol = 0
    					Case = "P3_DGV_1"
    						If (myCol = 0) Then
    							Me.P3_DGV_1.Rows.Add()
    							P3_DGV_1_RowsCnt += 1
    						End If
    						' --- Column-Loop: each column > P3_DGV_1
    						For Each currentData As String In currentRow
    							Me.P3_DGV_1(myCol, P3_DGV_1_RowsCnt).Value = currentData
    							myCol += 1
    						Next
    						' --- end of Column-Loop
    						myCol = 0
    					Case = "P3_DGV_2"
    						If (myCol = 0) Then
    							Me.P3_DGV_2.Rows.Add()
    							P3_DGV_2_RowsCnt += 1
    						End If
    						' --- Column-Loop: each column > P3_DGV_2
    						For Each currentData As String In currentRow
    							Me.P3_DGV_2(myCol, P3_DGV_2_RowsCnt).Value = currentData
    							myCol += 1
    						Next
    						' --- end of Column-Loop
    						myCol = 0
    				End Select
    			End While
    			' --- end of reading a file
    			textFile.Close()
    		End Using
    	Next
    End Sub

    Regards,

    Ashidacchi

    • Marked as answer by KernowKiwi Thursday, February 22, 2018 3:58 AM
    Thursday, February 22, 2018 2:45 AM

All replies

  • Hi,

    Would you please provide your code related with the import?

    Regards,

    Ashidacchi

    Wednesday, February 21, 2018 1:32 AM
  •  For Each sfile As String In Directory.GetFiles(sPath
     Using sr As StreamReader = New StreamReader(sfile)
        Do While sr.Peek() >= 0
            Dim sLine As String = sr.ReadLine()
            Dim aEntities() = sLine.Split("|")
            Dim intArrayLength As Integer = aEntities.Length - 1 
            Dim ctrl As DataGridView = FrmMain.Controls.Find(aEntities(0), True)(0) 
            Dim intRowNumber As Integer = ctrl.Rows.Add()
            Dim intColumn As Integer = 0  
            Dim intCount = 1
            Do Until intCount = intArrayLength + 1
                ctrl(intColumn, intRowNumber).Value = aEntities(intCount)
                intCount += 1
                intColumn += 1
            Loop
        Loop
        sr.Close()
    End Using
    Next
    Wednesday, February 21, 2018 1:44 AM
  • I would suggest trying TextFieldParser e.g.

    As noted in code, place a break-point, examine what is in the array currentRow. Also note the Delimiter property.

    Imports System.IO
    Public Class Form2
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim sPath As String = "your path"
            For Each sfile As String In Directory.GetFiles(sPath)
                Using myReader As New FileIO.TextFieldParser(sfile)
    
                    myReader.TextFieldType = FileIO.FieldType.Delimited
                    myReader.Delimiters = New String() {"|"}
                    Dim currentRow As String()
    
                    While Not myReader.EndOfData
                        Try
                            currentRow = myReader.ReadFields() ' place break-point here, examine values
                            DataGridView1.Rows.Add(currentRow)
                        Catch ex As FileIO.MalformedLineException
                            Console.WriteLine(ex.Message)
                        End Try
                    End While
                End Using
            Next
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, February 21, 2018 2:09 AM
    Moderator
  • How will this stop the first cell of the datagridview from not showing the value although it is all ready there ?
    Wednesday, February 21, 2018 4:11 AM
  • Hi,

    Thank you for providing code.
    I made a sample project.
       

    [note]
       (1) This code is in [Imports] button on Form 
       (2) DataGridView is named "P2_DGV" and defined in Designer.
       (3) A variable "myPath" is already provided. (shown on the upper right)
       (4) There are 2 files (*.txt) in "myPath".
            Each file contains 3 lines. (total 6 lines)
    ' ---[Import]
    Private Sub P2_btn_Import_Click(sender As Object, e As EventArgs) Handles P2_btn_Import.Click
    	Dim sPath As String = Me.myPath
    	Dim currentRow As String()  ' -- array for 1 Line/Row
    	Dim myRow As Integer = 0
    	Dim myCol As Integer = 0
    	' --- remove rows in DataGridView
    	Me.P2_DGV.Rows.Clear()
    	' --- process all files in a folder
    	For Each sfile As String In Directory.GetFiles(sPath)
    		If (IO.Path.GetExtension(sfile).ToLower <> ".txt") Then
    			Continue For ' -- only ".txt" processed
    		End If
    		Using textFile As New FileIO.TextFieldParser(sfile)
    			With textFile
    				.TextFieldType = FileIO.FieldType.Delimited
    				.SetDelimiters("|")
    			End With
    			' --- Row-Loop: read textFile
    			While Not textFile.EndOfData
    				currentRow = textFile.ReadFields()  ' -- store 1 line to array 
    				Me.P2_DGV.Rows.Add()                ' -- add Row
    				' --- Column-Loop: each column > DataGridView
    				For Each currentData As String In currentRow
    					Me.P2_DGV(myCol, myRow).Value = currentData
    					myCol += 1
    				Next
    				' --- end of Column-Loop
    				myCol = 0
    				myRow += 1
    			End While
    			' --- end of reading a file
    			textFile.Close()
    		End Using
    	Next
    End Sub

    Regards,

    Ashidacchi

    • Edited by Ashidacchi Wednesday, February 21, 2018 4:35 AM
    Wednesday, February 21, 2018 4:31 AM
  • Hi, thanks for the help so far.  I should have explained a little more.

    • Each text file uses the "|" symbol to separate data
    • The first entry on each line in the text file is the name of the DataGridView

              dgvName | SomeName | 1.0.01 | 10.0.1.1 | Somewhere | away.com

              dgvName | SomeName | 1.0.02 | 11.1.1.1 | Somewhere | home.com

    What I was trying to achieve was to:

    • Open the text file
    • Read the line
    • Get the first entry and then find the Datagridview using that name
    • Add a line to the DataGridView
    • Then iterate through the rest of the text file line to add the data into the relevant columns 

    Each line in the text file could have a different DataGridView name at the start, hence why I need to find the control.

    Wednesday, February 21, 2018 10:41 PM
  • Hi Karen

    The example above works but I still have the same issue that the cell (0,0) is still shown as empty unless I click into that cell where upon the text becomes highlighted and then diplayed

    Also, I need to exclude the first item in the text file row that is read as it is the name of the DatGridView.  It works with the code that I originally posted, but I have the same issue, in that the cell (0,0) is still shown as empty unless I click into 

    Wednesday, February 21, 2018 10:52 PM
  • Hi Karen

    The example above works but I still have the same issue that the cell (0,0) is still shown as empty unless I click into that cell where upon the text becomes highlighted and then diplayed

    Also, I need to exclude the first item in the text file row that is read as it is the name of the DatGridView.  It works with the code that I originally posted, but I have the same issue, in that the cell (0,0) is still shown as empty unless I click into 

    I suppose you tried .refresh or something?

    Wednesday, February 21, 2018 11:05 PM
  • Yes, made no difference 

    Bl***dy strange really...gonna be something so simple I expect

    Wednesday, February 21, 2018 11:16 PM
  • Hi,

    I will modify my code and check if it will work. Before checking, I would like to confirm:

    (1) Is your application executed several times? i.e. Is the importing data to the DataGridView executed more than once?

    (2) Is it enough for testing that I use two DGVs and two text files?

    Regards,


    Ashidacchi

    Wednesday, February 21, 2018 11:29 PM
  • Hi, I have a form with multiple tabs.  Each Tab has a separate DGV.  A file is created for each DGV.

    When the form is loaded all of the text files are read and populate the relevant DGV  

    So as an example, Tab1 has dgv1 on it.  text1.txt has the data that needs to be imported into the dgv

    hope that helps 

    Wednesday, February 21, 2018 11:54 PM
  • Hi, I have a form with multiple tabs.  Each Tab has a separate DGV.  A file is created for each DGV.

    When the form is loaded all of the text files are read and populate the relevant DGV  

    So as an example, Tab1 has dgv1 on it.  text1.txt has the data that needs to be imported into the dgv

    hope that helps 

    If you can make a simple one form example that will help solve the problem. Then everyone can play with it. Or you find the problem trying to make an example.

    For example can you duplicate it with just a dgv on a form and a couple of rows of hard data?

    If not maybe it is one of those start removing things until it stops type problem. Then you know the last thing your removed has the problem more or less etc.

    Thursday, February 22, 2018 1:08 AM
  • Hi,
    Don't you forget answering to my questions?
    Regards,


    Ashidacchi

    Thursday, February 22, 2018 1:38 AM
  • I did, at least I thought I did......

    I have a form with multiple tabs.  Each Tab has a separate DGV.  A file is created for each DGV.

    When the form is loaded all of the text files are read and populate the relevant DGV  

    So as an example, Tab1 has dgv1 on it.  text1.txt has the data that needs to be imported into the dgv

    The import of data occurs only once.  The data in the individual text files gets entered into the associated dgv.

    Thursday, February 22, 2018 1:50 AM
  • Hi, 

    You seems not understand why I ask question (its real meaning), but anyway, I made a sample.





    This code is in [Import] button.
    You can simply move this into Form_Load event (MyBase.Load). 

    There are 3 pages in a TabControl.
    DataGridView "P2_DGV_1" is in 2nd page,
    DataGridView "P3_DGV_1" and "P3_DGV_2" are in 3rd page,
    ' ---[Import]
    Private Sub P2_btn_Import_Click(sender As Object, e As EventArgs) Handles P2_btn_Import.Click
    	Dim P2_DGV_1_RowsCnt As Integer = -1
    	Dim P3_DGV_1_RowsCnt As Integer = -1
    	Dim P3_DGV_2_RowsCnt As Integer = -1
    	' ---
    	Dim sPath As String = Me.myPath
    	Dim currentRow As String()  ' -- array for 1 Line/Row
    	Dim myCol As Integer = 0
    	' --- process all files in a folder
    	For Each sfile As String In Directory.GetFiles(sPath)
    		If (IO.Path.GetExtension(sfile).ToLower <> ".txt") Then
    			Continue For ' -- .txt 以外は対象外
    		End If
    		Using textFile As New FileIO.TextFieldParser(sfile)
    			With textFile
    				.TextFieldType = FileIO.FieldType.Delimited
    				.SetDelimiters("|")
    			End With
    			' --- Row-Loop: read textFile
    			While Not textFile.EndOfData
    				currentRow = textFile.ReadFields()  ' -- store 1 line to array 
    				' --- add Row
    				Select Case currentRow(0)
    					Case = "P2_DGV_1"
    						If (myCol = 0) Then
    							Me.P2_DGV_1.Rows.Add()
    							P2_DGV_1_RowsCnt += 1
    						End If
    						' --- Column-Loop: each column > P2_DGV_1
    						For Each currentData As String In currentRow
    							Me.P2_DGV_1(myCol, P2_DGV_1_RowsCnt).Value = currentData
    							myCol += 1
    						Next
    						' --- end of Column-Loop
    						myCol = 0
    					Case = "P3_DGV_1"
    						If (myCol = 0) Then
    							Me.P3_DGV_1.Rows.Add()
    							P3_DGV_1_RowsCnt += 1
    						End If
    						' --- Column-Loop: each column > P3_DGV_1
    						For Each currentData As String In currentRow
    							Me.P3_DGV_1(myCol, P3_DGV_1_RowsCnt).Value = currentData
    							myCol += 1
    						Next
    						' --- end of Column-Loop
    						myCol = 0
    					Case = "P3_DGV_2"
    						If (myCol = 0) Then
    							Me.P3_DGV_2.Rows.Add()
    							P3_DGV_2_RowsCnt += 1
    						End If
    						' --- Column-Loop: each column > P3_DGV_2
    						For Each currentData As String In currentRow
    							Me.P3_DGV_2(myCol, P3_DGV_2_RowsCnt).Value = currentData
    							myCol += 1
    						Next
    						' --- end of Column-Loop
    						myCol = 0
    				End Select
    			End While
    			' --- end of reading a file
    			textFile.Close()
    		End Using
    	Next
    End Sub

    Regards,

    Ashidacchi

    • Marked as answer by KernowKiwi Thursday, February 22, 2018 3:58 AM
    Thursday, February 22, 2018 2:45 AM