Results 1 to 4 of 4

Thread: [RESOLVED] Datagrid to .txt (array problem)

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    12

    Resolved [RESOLVED] Datagrid to .txt (array problem)

    Hi everyone I'm fairly inexperienced with VB but have been trying to get "save" functionality out of a program I'm trying to write.

    Basically I have a Datagridview and I want to write each line from the grid into a .txt file. So what I've been trying to do is use Datagridview.Items and then variables as column / row indexes to move cell by cell left to right , writing each cells data to an array which also increments along the way. Then using WriteLine to write the array to the text file, zero'ing all the counters then starting all over again for the next row.... make sense?

    Now .. I keep getting an error sayin im using the "gridData(DataColumn)" array before I've assigned a value to it... thing is this error occurs on the code line where I am assigning a value to it?!

    Here's the code:

    Code:
            Dim gridData()
    
            Dim DataColumn, DataRow, i As Integer
    
            DataRow = 0
            i = 0
    
            FileOpen(1, FileName, OpenMode.Output)
    
    
            Do Until DataRow = DataGridView1.RowCount()
    
                For DataColumn = 0 To DataGridView1.ColumnCount
    
                    gridData(DataColumn) = CStr(DataGridView1.Item(DataColumn, DataRow).Value) '<--- error happens here?'
                    DataColumn = DataColumn + 1
    
                Next
    
                WriteLine(1, gridData)
                DataRow = DataRow + 1
    
            Loop

    Hoping someone can help me sort this out? I am well aware that life would be easier if I bound the grid to a database but to be honest the fact that this is harder to do as a text file makes me more interested and if I was to share the finished product with friend ect I wouldn't expect them all to have access ect installed...

    I look forward to responses !

    Many Thanks in Advance!

    (Using VB 2008 Express)

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Datagrid to .txt (array problem)

    You need to initialize your array before you use it. Right now gridData is nothing.

    vb.net Code:
    1. Dim gridData(DataGridView1.RowCount - 1) As String

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Datagrid to .txt (array problem)

    try this:

    vb Code:
    1. Dim gridData(dgv1.Rows.Count - 1) As String
    2. For Each r As DataGridViewRow In dgv1.Rows
    3.     If r.IsNewRow Then Exit For
    4.     Dim cellValues(dgv1.Rows(r.Index).Cells.Count - 1) As String
    5.     For Each c As DataGridViewCell In dgv1.Rows(r.Index).Cells
    6.         cellValues(c.ColumnIndex) = c.Value.ToString
    7.     Next
    8.     gridData(r.Index) = String.Join(",", cellValues)
    9. Next
    10. Dim lines = From line In gridData _
    11.             Where line <> Nothing _
    12.             Select line
    13.  
    14. IO.File.WriteAllLines("gridData.txt", lines.ToArray)
    15. Process.Start("gridData.txt")

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    12

    Re: Datagrid to .txt (array problem)

    Think for your replies guys, much appreciated

    I managed to get it all working in the end, code looks like this:

    Code:
    
            Dim gridData(DataGridView1.ColumnCount - 1) As String
    
            Dim DataColumn, DataRow, ColumnCount As Integer
    
            DataRow = 0
    
            FileOpen(1, FileName, OpenMode.Output)
    
            ColumnCount = DataGridView1.ColumnCount
    
            Do Until DataRow = DataGridView1.RowCount() - 1
    
                For DataColumn = 0 To ColumnCount - 1
    
                    gridData(DataColumn) = CStr(DataGridView1.Item(DataColumn, DataRow).Value)
    
                Next
    
                WriteLine(1, gridData)
    
                DataRow = DataRow + 1
    
            Loop
    
            FileClose(1)


    Just hope other people can make some use of this ...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width