7 Replies - 2518 Views - Last Post: 22 October 2008 - 06:07 AM Rate Topic: -----

#1 whoisit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 15-October 08

Gridview to txt file?

Posted 21 October 2008 - 04:02 AM

Here's what I have although it saves to a txt file it is not the text that is in he cells of the grid.
How do i get it to work?
If SaveFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
			Dim outputstream As StreamWriter = File.CreateText(SaveFileDialog.FileName)

			Dim iRow, iCol As Integer
			Dim name, width, height, number, code, emotion As String
			With DataGridView1
				For iRow = 0 To .Rows.Count - 1
					For iCol = 0 To .Columns.Count - 1


						name = .Rows(iRow).ToString
						width = .Rows(iRow).ToString
						height = .Rows(iRow).ToString
						number = .Rows(iRow).ToString
						code = .Rows(iRow).ToString
						emotion = .Rows(iRow).ToString

						outputstream.WriteLine("'" & name & "','" & width & "','" & height & "','" & number & "','" & code & "','" & emotion & "',")

					Next iCol
				Next iRow
			End With
			outputstream.Close()
		End If


Is This A Good Question/Topic? 0
  • +

Replies To: Gridview to txt file?

#2 magicmonkey  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: Gridview to txt file?

Posted 21 October 2008 - 07:45 AM

Maybe...

        Dim iRow, iCol As Integer
        Dim name, width, height, number, code, emotion As String
        With DataGridView1
            For iRow = 0 To .Rows.Count - 1
                For iCol = 0 To .Columns.Count - 1
                    If iCol > 0 Then outputstream.Write(",") 'Add comma after first column 
                    outputstream.Write("'" & DataGridView1.Rows(iRow).Cells(iCol).ToString & "'")
                Next iCol
                outputstream.WriteLine() 'Add linefeed
            Next iRow
        End With
        outputstream.Close()


Was This Post Helpful? 0
  • +
  • -

#3 whoisit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 15-October 08

Re: Gridview to txt file?

Posted 21 October 2008 - 09:44 AM

View Postmagicmonkey, on 21 Oct, 2008 - 07:45 AM, said:

Maybe...

        Dim iRow, iCol As Integer
        Dim name, width, height, number, code, emotion As String
        With DataGridView1
            For iRow = 0 To .Rows.Count - 1
                For iCol = 0 To .Columns.Count - 1
                    If iCol > 0 Then outputstream.Write(",") 'Add comma after first column 
                    outputstream.Write("'" & DataGridView1.Rows(iRow).Cells(iCol).ToString & "'")
                Next iCol
                outputstream.WriteLine() 'Add linefeed
            Next iRow
        End With
        outputstream.Close()



This is saving the same as my code,(not as many lines and neater) :D
I tried just copy and pasting your code in to my program and it showed squiglies under name, width, etc with unused local variable. I ran it anyway to try it, same result.
I tried a mix and match with both codes in various combinations but same result.
Below is how what is saved n the txt file.
So any other thoughts on this one?
You think it would be easy, Save what is in Row(0), Column(0) as text; Row(0), Column(1) as text, etc,etc and just loop through.

'DataGridViewTextBoxCell { ColumnIndex=0, RowIndex=0 }','DataGridViewTextBoxCell { ColumnIndex=1, RowIndex=0 }','DataGridViewTextBoxCell { ColumnIndex=2, RowIndex=0 }','DataGridViewTextBoxCell { ColumnIndex=3, RowIndex=0 }','DataGridViewTextBoxCell { ColumnIndex=4, RowIndex=0 }','DataGridViewTextBoxCell { ColumnIndex=5, RowIndex=0 }'
'DataGridViewTextBoxCell { ColumnIndex=0, RowIndex=1 }','DataGridViewTextBoxCell { ColumnIndex=1, RowIndex=1 }','DataGridViewTextBoxCell { ColumnIndex=2, RowIndex=1 }','DataGridViewTextBoxCell { ColumnIndex=3, RowIndex=1 }','DataGridViewTextBoxCell { ColumnIndex=4, RowIndex=1 }','DataGridViewTextBoxCell { ColumnIndex=5, RowIndex=1 }'
'DataGridViewTextBoxCell { ColumnIndex=0, RowIndex=2 }','DataGridViewTextBoxCell { ColumnIndex=1, RowIndex=2 }','DataGridViewTextBoxCell { ColumnIndex=2, RowIndex=2 }','DataGridViewTextBoxCell { ColumnIndex=3, RowIndex=2 }','DataGridViewTextBoxCell { ColumnIndex=4, RowIndex=2 }','DataGridViewTextBoxCell { ColumnIndex=5, RowIndex=2 }'

Was This Post Helpful? 0
  • +
  • -

#4 magicmonkey  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: Gridview to txt file?

Posted 21 October 2008 - 11:47 AM

Hehe my bad... forgot to add the .Value to the end, and you don't need those vars.

        Dim iRow, iCol As Integer
        With DataGridView1
            For iRow = 0 To .Rows.Count - 1
                For iCol = 0 To .Columns.Count - 1
                    If iCol > 0 Then outputstream.Write(",") 'Add comma after first column   
                    outputstream.Write("'" & DataGridView1.Rows(iRow).Cells(iCol).Value.ToString & "'") 'Need to get the Value
                Next iCol
                outputstream.WriteLine() 'Add linefeed  
            Next iRow
        End With
        outputstream.Close()


This post has been edited by magicmonkey: 21 October 2008 - 11:49 AM

Was This Post Helpful? 0
  • +
  • -

#5 magicmonkey  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: Gridview to txt file?

Posted 21 October 2008 - 12:01 PM

Same things just a bit cleaner...

        For iRow As Integer = 0 To DataGridView1.Rows.Count - 1
            For iCol As Integer = 0 To DataGridView1.Columns.Count - 1
                If iCol > 0 Then OutputStream.Write(",")
                OutputStream.Write("'{0}'", DataGridView1.Rows(iRow).Cells(iCol).Value)
            Next
            OutputStream.WriteLine()
        Next
        OutputStream.Close()


Was This Post Helpful? 1
  • +
  • -

#6 whoisit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 15-October 08

Re: Gridview to txt file?

Posted 22 October 2008 - 01:38 AM

View Postmagicmonkey, on 21 Oct, 2008 - 12:01 PM, said:

Same things just a bit cleaner...

        For iRow As Integer = 0 To DataGridView1.Rows.Count - 1
            For iCol As Integer = 0 To DataGridView1.Columns.Count - 1
                If iCol > 0 Then OutputStream.Write(",")
                OutputStream.Write("'{0}'", DataGridView1.Rows(iRow).Cells(iCol).Value)
            Next
            OutputStream.WriteLine()
        Next
        OutputStream.Close()



Thanks Magic, :^:
Please don't think I just come here, ask for code, copy and paste it, then come back for more (well OK, maybe I do)
but I also take the time to figure out why the code you write does what it does by using step through.
I do have one question, make that two.
1)
When the file is saved it always writes a last line like this,
'','','','','',''
I figure it's because when I load the GridView it automaticaly adds a blank row at the end, ready for more data to be added, is there a way to stop this?
I'm going to go and have a look, but in case I can't figure it, I know I can come back here for help.

2)
My data Grid loads the information from a folder of images,
'Cold.gif','34','36','0',':Cold:','Cold'
Is it best to have the images load in the first column of the grid?
or is it best to let the user click the first cell 'Cold.gif' and show the image in a pictur box?

Thanks Again
Graham
Was This Post Helpful? 0
  • +
  • -

#7 magicmonkey  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: Gridview to txt file?

Posted 22 October 2008 - 05:25 AM

1) Look at the DataGridView1.Rows(iRow) properties for a property that tells you it is a new row and don't write out to the stream if it is. Or even better disable the new row in the grid as you dont want hte users trying to add new rows?

2) That comes down to how you want the application to work/look. I would suggest displaying maybe a thumbnail in the grid, but add a toolbar button to toggle it off/on. I would still allow the user to view the image in a large picture box when they select it\dbl click on it.
Was This Post Helpful? 1
  • +
  • -

#8 whoisit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 15-October 08

Re: Gridview to txt file?

Posted 22 October 2008 - 06:07 AM

View Postmagicmonkey, on 22 Oct, 2008 - 05:25 AM, said:

1) Look at the DataGridView1.Rows(iRow) properties for a property that tells you it is a new row and don't write out to the stream if it is. Or even better disable the new row in the grid as you dont want hte users trying to add new rows?

2) That comes down to how you want the application to work/look. I would suggest displaying maybe a thumbnail in the grid, but add a toolbar button to toggle it off/on. I would still allow the user to view the image in a large picture box when they select it\dbl click on it.



Thanks Magic,

1) Soon sorted!

2) Will go away and have a go at this, sounds dead easy, so I probabaly will never be back here again,
and if you believe that you can knit fog! :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1