DatagridView not populating

dinotom

Member
Joined
Sep 5, 2011
Messages
11
Programming Experience
3-5
I have a form which the datagridview will be built manually by reading in certain data from a text file (this is because the data in the text file will change periodically)
The code is correctly adding the proper amount of rows but it is NOT putting the required values in the cells. Any thoughts?

VB.NET:
 [COLOR=blue]Sub[/COLOR] Main()        [COLOR=green][FONT=Lucida Sans][I]' Get a DataTable instance from helper function.[/I][/FONT][/COLOR]        [COLOR=blue]Dim[/COLOR] dt [COLOR=blue]As[/COLOR] [COLOR=blue]New[/COLOR] [COLOR=#2b91af]DataTable[/COLOR]        dt = GetTable()        BindingSource1.DataSource = dt        datagridADRORD.DataSource = BindingSource1    [COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR]

VB.NET:
[COLOR=blue]Function[/COLOR] GetTable() [COLOR=blue]As[/COLOR] [COLOR=#2b91af]DataTable[/COLOR]        [COLOR=green][FONT=Lucida Sans][I]' Create new DataTable instance.[/I][/FONT][/COLOR]        [COLOR=blue]Dim[/COLOR] table [COLOR=blue]As[/COLOR] [COLOR=blue]New[/COLOR] [COLOR=#2b91af]DataTable[/COLOR]        [COLOR=green][FONT=Lucida Sans][I]' Create four typed columns in the DataTable.[/I][/FONT][/COLOR]        table.Columns.Add([COLOR=#a31515]"ADR Symbol"[/COLOR], [COLOR=blue]GetType[/COLOR]([COLOR=blue]String[/COLOR]))        table.Columns.Add([COLOR=#a31515]"ADR Price"[/COLOR], [COLOR=blue]GetType[/COLOR]([COLOR=blue]Double[/COLOR]))        table.Columns.Add([COLOR=#a31515]"ORD Symbol"[/COLOR], [COLOR=blue]GetType[/COLOR]([COLOR=blue]String[/COLOR]))        table.Columns.Add([COLOR=#a31515]"ORD Price"[/COLOR], [COLOR=blue]GetType[/COLOR]([COLOR=blue]Double[/COLOR]))        table.Columns.Add([COLOR=#a31515]"Ratio"[/COLOR], [COLOR=blue]GetType[/COLOR]([COLOR=blue]Double[/COLOR]))        table.Columns.Add([COLOR=#a31515]"Currency"[/COLOR], [COLOR=blue]GetType[/COLOR]([COLOR=blue]String[/COLOR]))        table.Columns.Add([COLOR=#a31515]"Currency Price"[/COLOR], [COLOR=blue]GetType[/COLOR]([COLOR=blue]Double[/COLOR]))        table.Columns.Add([COLOR=#a31515]"Difference"[/COLOR], [COLOR=blue]GetType[/COLOR]([COLOR=blue]Double[/COLOR]))        table.Columns.Add([COLOR=#a31515]"GoNoGo"[/COLOR], [COLOR=blue]GetType[/COLOR]([COLOR=blue]String[/COLOR]))         [COLOR=green][FONT=Lucida Sans][I]'start to loop thru all the symbols in the symbols file[/I][/FONT][/COLOR]        [COLOR=green][FONT=Lucida Sans][I]' first read them into an array[/I][/FONT][/COLOR]        [COLOR=green][FONT=Lucida Sans][I]'the line below is for the commodity symbols file [/I][/FONT][/COLOR]        [COLOR=blue]Dim[/COLOR] strText [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR] = [COLOR=#2b91af]File[/COLOR].ReadAllText([COLOR=#a31515]"F:\ADR-ORD Program\ADR-ORD Comparator\ADR-ORD Comparator\SymbolsinBBRGFormat.txt"[/COLOR])        [COLOR=blue]Dim[/COLOR] aryText() [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR] = strText.Split([COLOR=#a31515]";"[/COLOR])        strText = vbNullString        [COLOR=blue]Dim[/COLOR] x [COLOR=blue]As[/COLOR] [COLOR=blue]Integer[/COLOR]        [COLOR=blue]Dim[/COLOR] symADR [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR] = [COLOR=#a31515]""[/COLOR], symORD [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR] = 0, strCurncy [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR] = [COLOR=#a31515]""[/COLOR]        [COLOR=blue]Dim[/COLOR] dblADRPx [COLOR=blue]As[/COLOR] [COLOR=blue]Double[/COLOR] = 0, dblORDPx [COLOR=blue]As[/COLOR] [COLOR=blue]Double[/COLOR] = 0, dblCurncyPx [COLOR=blue]As[/COLOR] [COLOR=blue]Double[/COLOR] = 0        [COLOR=blue]Dim[/COLOR] ratio [COLOR=blue]As[/COLOR] [COLOR=blue]Double[/COLOR] = 1, diff [COLOR=blue]As[/COLOR] [COLOR=blue]Double[/COLOR] = 0, goNogo [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR] = [COLOR=#a31515]""[/COLOR]         [COLOR=blue]For[/COLOR] x = 0 [COLOR=blue]To[/COLOR] UBound(aryText) - 1                     symADR = Split(aryText(x), [COLOR=#a31515]","[/COLOR])(0)            dblADRPx = 1 [COLOR=green][FONT=Lucida Sans][I]'add bbrg formula here?[/I][/FONT][/COLOR]            symORD = Split(aryText(x), [COLOR=#a31515]","[/COLOR])(1)            dblORDPx = 1 [COLOR=green][FONT=Lucida Sans][I]' add bbrg formula here?[/I][/FONT][/COLOR]            ratio = Split(aryText(x), [COLOR=#a31515]","[/COLOR])(2)            strCurncy = Split(aryText(x), [COLOR=#a31515]","[/COLOR])(3)            dblCurncyPx = 1            diff = 1            goNogo = [COLOR=#a31515]"Go"[/COLOR]             [COLOR=green][FONT=Lucida Sans][I]' Add rows of data for those columns filled in the DataTable.[/I][/FONT][/COLOR]            table.Rows.Add(symADR, dblADRPx, symORD, dblORDPx, ratio, strCurncy, dblCurncyPx, diff, goNogo)         [COLOR=blue]Next[/COLOR] x        [COLOR=blue]Return[/COLOR] table    [COLOR=blue]End[/COLOR] [COLOR=blue]Function[/COLOR]
 
Please post your code properly so that we can read it. Post it as plain text inside
 tags.
 
Reposting the code. I hope it works properly

Sub Main()
      ' Get a DataTable instance from helper function.
      Dim dt As New DataTable
      dt = GetTable()
      BindingSource1.DataSource = dt
      datagridADRORD.DataSource = BindingSource1
 End Sub


Function GetTable() As DataTable
 ' Create new DataTable instance.
 Dim table As New DataTable
 ' Create typed columns in the DataTable.
 table.Columns.Add("ADR Symbol", GetType(String))
        table.Columns.Add("ADR Price", GetType(Double))
        table.Columns.Add("ORD Symbol", GetType(String))
        table.Columns.Add("ORD Price", GetType(Double))
        table.Columns.Add("Ratio", GetType(Double))
        table.Columns.Add("Currency", GetType(String))
        table.Columns.Add("Currency Price", GetType(Double))
        table.Columns.Add("Difference", GetType(Double))
        table.Columns.Add("GoNoGo", GetType(String))


'start to loop thru all the symbols in the symbols file
' first read them into an array
  Dim strText As String = File.ReadAllText("F:\SymbolsinBBRGFormat.txt")
  Dim aryText() As String = strText.Split(";")
  strText = vbNullString
  Dim x As Integer
  Dim symADR As String = "", symORD As String = 0, strCurncy As String = ""
  Dim dblADRPx As Double = 0, dblORDPx As Double = 0, dblCurncyPx As Double = 0
  Dim ratio As Double = 1, diff As Double = 0, goNogo As String = ""
 	
	 For x = 0 To UBound(aryText) - 1
                 symADR = Split(aryText(x), ",")(0)
                 dblADRPx = 1 'add bbrg formula here?
                 symORD = Split(aryText(x), ",")(1)
                 dblORDPx = 1 ' add bbrg formula here?
                 ratio = Split(aryText(x), ",")(2)
                 strCurncy = Split(aryText(x), ",")(3)
                 dblCurncyPx = 1            diff = 1
                 goNogo = "Go" ' Add rows of data for those columns filled in the DataTable.
	'while debugging I confirmed that all these variables get data but the data doesnt end up in the gridview cells
	'from the code line below
            table.Rows.Add(symADR, dblADRPx, symORD, dblORDPx, ratio, strCurncy, dblCurncyPx, diff, goNogo)
         Next x
  Return table
End Function

 
Last edited by a moderator:
No, it didn't work but I have now fixed it. You put a
tag inside your [xcode] tag and you didn't close your [xcode] tag properly. You might like to click the EDIT POST button on your post to see what it looks like done properly, for future reference.

I will try to have a look at your issue some time soon.
 
Pretty sure that datatable.rows.add takes a datarow object and not field values as you're trying.

Create yourself a datarow object using .NewRow e.g

VB.NET:
Dim newTableRow as datarow = table.NewRow()

then assign your field values to the newTableRow object and finally add that row to your datatable
 
You appear to be reading a CSV file, but in quite a simplistic way that will break if any of your data ever contains a comma. You might be better off using TextFieldParser, or even easier for you, use the Jet Database driver to connect to your CSV file as a database and read it. There are a few tutorials in the DW4 link in my signature for doing this (start with Creating a Simple Data App).. The important difference is that instead of using an access database, youre using the Jet OLEDB driver in text mode.. You can check connectionstrings.com for the relevant connection string for your driver in this case
 
Apologies for the lack of responses but I was away. I have solved the problems with all your help. I will post the code properly going forward.
 
Back
Top