[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)
Re: Datagrid to .txt (array problem)
You need to initialize your array before you use it. Right now gridData is nothing.
vb.net Code:
Dim gridData(DataGridView1.RowCount - 1) As String
Re: Datagrid to .txt (array problem)
try this:
vb Code:
Dim gridData(dgv1.Rows.Count - 1) As String
For Each r As DataGridViewRow In dgv1.Rows
If r.IsNewRow Then Exit For
Dim cellValues(dgv1.Rows(r.Index).Cells.Count - 1) As String
For Each c As DataGridViewCell In dgv1.Rows(r.Index).Cells
cellValues(c.ColumnIndex) = c.Value.ToString
Next
gridData(r.Index) = String.Join(",", cellValues)
Next
Dim lines = From line In gridData _
Where line <> Nothing _
Select line
IO.File.WriteAllLines("gridData.txt", lines.ToArray)
Process.Start("gridData.txt")
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 ...