6 Replies - 2753 Views - Last Post: 16 January 2014 - 05:58 AM Rate Topic: -----

#1 luna12  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 08-December 13

Datagridview to multiple .txt files

Posted 15 January 2014 - 09:54 PM

Iím using a datagridview to save multiple .txt files to a folder. Column 1 is the filename and column 2 is the content of the file.

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        For Each row As DataGridViewRow In DataGridView1.SelectedRows
            My.Computer.FileSystem.WriteAllText("D:\samplefolder\" & DataGridView1.SelectedCells(0).Value.ToString & ".txt", DataGridView1.SelectedCells(1).Value.ToString, False)
        Next
    End Sub


Iím trying to loop through the entire datagridview, but the only .txt file being produced is the one where the little arrow is in the row header. This happens even if I have multiple rows highlighted/selected. How do I make .txt files for all of the selected rows? Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Datagridview to multiple .txt files

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13657
  • View blog
  • Posts: 54,501
  • Joined: 12-June 08

Re: Datagridview to multiple .txt files

Posted 15 January 2014 - 09:58 PM

Perhaps don't use selectedrows and just use 'rows'.
Never mind... it seems that is not what you want.

What happens when you put a breakpoint in that for loop after selecting multiple rows? What's the count of that selectedrow collection?

Also - when dealing with rows don't use "DataGridView1.SelectedCells(1)." unless you want the current selected cell to be the only value for a name. Use your 'row' loop control variable and the cell reference from that.
Was This Post Helpful? 0
  • +
  • -

#3 luna12  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 08-December 13

Re: Datagridview to multiple .txt files

Posted 15 January 2014 - 10:03 PM

I'm not sure how to do a breakpoint. The count on the row collection will vary depending on how many rows the user makes. Here's a link to a general picture. http://social.msdn.m.../getfile/397862
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13657
  • View blog
  • Posts: 54,501
  • Joined: 12-June 08

Re: Datagridview to multiple .txt files

Posted 15 January 2014 - 10:10 PM

Okay.. well fix where you are calling the cell for the name.. and see what happens.. Don't forget collections and arrays start their count at 0.. so the first item in the collection is at index 0.
Was This Post Helpful? 0
  • +
  • -

#5 luna12  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 08-December 13

Re: Datagridview to multiple .txt files

Posted 15 January 2014 - 10:52 PM

The following code is just what I needed:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    For Each row As DataGridViewRow In DataGridView1.SelectedRows
        My.Computer.FileSystem.WriteAllText("D:\samplefolder\" & row.Cells(0).Value.ToString & ".txt", row.Cells(1).Value.ToString, False)
    Next row
End Sub

Was This Post Helpful? 0
  • +
  • -

#6 belgarion262  Icon User is offline

  • Prince of all (2) Saiyans

Reputation: 172
  • View blog
  • Posts: 955
  • Joined: 25-October 09

Re: Datagridview to multiple .txt files

Posted 16 January 2014 - 03:53 AM

View Postluna12, on 16 January 2014 - 05:03 AM, said:

I'm not sure how to do a breakpoint.


This is kinda a vital part of debugging your code. I realise you've now solved your issue, but it would be a good idea for you to look into it. Maybe here and here .
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is online

  • blow up my boots
  • member icon

Reputation: 6444
  • View blog
  • Posts: 26,064
  • Joined: 12-December 12

Re: Datagridview to multiple .txt files

Posted 16 January 2014 - 05:58 AM

I have a debugging tutorial here.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1