good evening all:)

first I want to start off by apologizing for my savefiledialog post..I did not mean to break forum rules regarding format for coded posts.

I have decided to revamp my project entirely and have decided to make a few combo boxes on a windows form. What I need it to do is to display the choices made from my comboboxes on a form (either the same one or a different form, it doesnt matter) AND I want to save the choices made in the combobox into a file. This is not a internet project, this is strictly an inhouse project that will be running on several computers.

Can anyone help point me in the right direction here?

Recommended Answers

All 49 Replies

combobox1.GetItemText(combobox1.SelectedItem) returns the text for the currently selected item.

This writes a line of string into a file.

Private Sub SaveToFile(fileName As String, data As String)
   Dim stream As New System.IO.FileStream(fileName, FileMode.Create)
   Dim sw As New System.IO.StreamWriter(stream)
   sw.WriteLine(data)
   sw.Flush()
   sw.Close()
End Sub

hello :) and thank you for the response. I would like to give you more detail as to what I am wanting to do in the hopes of getting more help from you. I am fairly new at all of this (about 3 weeks) and am really on a baby level getting my feets wet! lol..

what I am attempting to do is make a form for a tire shop. I want to put comboboxes on the left side of the form that the employees make choices from. I then want to put some kind of a table or grid looking thing on the right side of the form that will display their choices so that everyone in the shop can look at it see that "John is on bay 5 until 3:00pm".

at the end of the day I want all of the combobox choices to save to a file..I dont really need the table or the grid thing to save..just the choices.

If I want to do this, do I need to use that gridview tool? or can I make my own table thing to display the choices in. I really dont know anything about it..I have been reading about bound items and unbound Items all morning..is this what I am suppose to be doing?

Well. Unless you use a database of some sort as a backend, you don't have any real need for binding.

Here's what you can do.
Use the ListView control. It can be configured in a detail mode. Like the Windows Explorer.
Then, if you know beforehand what different information the list will contain, you can manually add columns in design mode.
Once that is done, it's a fairly simple matter to add items (rows) to the list. Like this:

Private Sub TheLastComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TheLastComboBox.SelectedIndexChanged
    Dim item As New ListViewItem 'The very first column in the list
    Dim subItem As New ListViewItem.ListViewSubItem 'Subsequent columns in the list

    item.Text = TheFirstComboBox.GetItemText(TheFirstComboBox.SelectedItem)
    subItem.Text = TheSecondComboBox.GetItemText(TheSecondComboBox.SelectedItem)
    item.SubItems.Add(subItem)

    subItem = New ListViewItem.ListViewSubItem
    subItem.Text = TheThirdComboBox.GetItemText(TheThirdComboBox.SelectedItem)
    item.SubItems.Add(subItem)

    ' And so on, and so on

    ListView1.Items.Add(item)
End Sub

At the end of the day you will have a ListView with a whole bunch of items (rows).
So, then add a button to store all that into a file by iterating (loop) through all the rows.

Private Sub SaveButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SaveButton.Click
   Dim stream As New System.IO.FileStream(fileName, IO.FileMode.Append, IO.FileAccess.Write)
    Dim sw As New System.IO.StreamWriter(stream)

    'Here we automatically create a CSV formatted text file
    'which in turn can be used to import into Excel. Cool, huh?
    For Each item As ListViewItem In ListView1.Items
        Dim line As String = ""
        line = item.Text
        line &= ";" & item.SubItems(0).Text
        line &= ";" & item.SubItems(1).Text
        line &= ";" & item.SubItems(2).Text
        sw.WriteLine(line)
    Next

    sw.Flush()
    sw.Close()
End Sub

Your right..I dont have a database I am doing this from. This is all from scratch that I would love to save to excel file at end of day...so that was a great addition you put :)

I went ahead and installed a datagridview onto the form. On the left side of the form are my drop down boxes (Time In, Employee, Task, Ect.) and then for the columns in the dataview..I named them accordingly to match the comboxes.

my question back to you is if I am not using a listview (only cuz i incorporated the dataview already)

How can I make the selections from the combobox show in the gridview? What you showed me for the listview worked pretty good..but is it the same way for a gridview?

AND..can i still use your code for saving to an excel file with a gridview? (that is sucha bonus for me! thank you for showing me that)

Yes. The principle is the same for both ListView and DataGridView.
Note that DataGridViewCell doesn't have a constructor.

Populate gridview:

Private Sub TheLastComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TheLastComboBox.SelectedIndexChanged
    Dim row As New DataGridViewRow 'The row object
    Dim cell As DataGridViewCell 'The columns

    cell.Value = TheFirstComboBox.GetItemText(TheFirstComboBox.SelectedItem)
    row.Cells.Add(cell)

    cell.Value = TheSecondComboBox.GetItemText(TheSecondComboBox.SelectedItem)
    row.Cells.Add(cell)

    cell.Value = TheThirdComboBox.GetItemText(TheThirdComboBox.SelectedItem)
    row.Cells.Add(subItem)

    ' And so on, and so on

    DataGridView.Rows.Add(row)
End Sub

And to store to file:

Private Sub SaveButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SaveButton.Click
   Dim stream As New System.IO.FileStream(fileName, IO.FileMode.Append, IO.FileAccess.Write)
    Dim sw As New System.IO.StreamWriter(stream)

    'Here we automatically create a CSV formatted text file
    'which in turn can be used to import into Excel. Cool, huh?
    For Each row As DataGridViewRow In DataGridView1.Rows
        Dim line As String = ""
        line = item.Text
        line &= ";" & row.Cells(0).Value.ToString()
        line &= ";" & row.Cells(1).Value.ToString()
        line &= ";" & row.Cells(2).Value.ToString()
        sw.WriteLine(line)
    Next

    sw.Flush()
    sw.Close()
End Sub

ahh thank ya :)

I will get at it this morning and let ya know how it turned out!

in the saving part..on the button click event:

it is telling me Name "filename" is not declared
and it is telling me Name "Item" is not declared.

I am sure it is something silly simple, but being so new at this I am not sure how to fix this.

And..i dont know how this is going to work, because I dont see any excel extensions? I dont understand how this is going to save into excel unless I first go into excel and set up a database file for this to save to?

Ah. Of cource. I'm sorry.
Instead of fileName you can enter either a string containing a full path and/or filename or add a SaveFile dialog to your form for selecting a path and filename.
And this part of the code should read:

For Each row As DataGridViewRow In DataGridView1.Rows
        Dim line As String = ""
        line = row.Cells(0).Value.ToString()
        line &= ";" & row.Cells(1).Value.ToString()
        line &= ";" & row.Cells(2).Value.ToString()
        line &= ";" & row.Cells(3).Value.ToString()
        sw.WriteLine(line)
    Next

I dunno Oxiegen..I am just tooo new at this to understand what your telling me. I have tried so hard to figure it out :(

This is where I am at now..
I have taken and got the combobox selected choices to display in the gridview. so that part is solved.

Then I went and followed a tutorial by timlayton to set up a database in my project.

so now what I want to do is save the contents of the gridview to the database that I created.

All morning long I have been googling on how to do this, but cant get anywhere with it :(.

Everyone seems to want to save a database to a gridview...lol and I wanna go the other way around!

Really all I want is to be able to save the contents of the gridview to a file, but figure it would be a great bonus if I can get the gridview to save to the database!

I am needing someone to help me that can keep it on a baby level with me for now. And you have done just that...but, I cannot figure out fileName error. I did try the code you posted this morning, cuz man o man the user that is gonna be getting this project..uses excel..but it is not a necessity, just a bonus. (and a big one)

I tried in the fileName error, to replace it with the name of the database but that didnt work either *sigh*

gooodness,,, am I gonna just have to scrap the whole idea of saving to a database and just try to save the conents of the gridview as a txt file..to open up in notepad? ..All that will give me is a buncha column lists. And while it will all be intact, Im sure she wont be happy with that.

I really appreciate all the help your giving me on this, and it is for sure not your problem and you have explained it all quite well..tis just my lack of experience and understanding that is inhibiting me to accomplish this.

Ok. Nice and simple.
Forget about the database for now. Here's what you do.

Move the save-file code to a separete method.
Then in your SaveButton click event, create a SaveFileDialog and
call the save-file method with the filename from the SaveFileDialog.
Like this:

Private Sub SaveButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SaveButton.Click
   Dim fileName As String = ""
   Dim dlgSave As New SaveFileDialog

   If dlgSave.ShowDialog = Windows.Forms.DialogResult.OK Then
      fileName = dlgSave.FileName
      SaveToFile(fileName)
   End If
End Sub

Private Sub SaveToFile(ByVal fileName As String)
   Dim stream As New System.IO.FileStream(fileName, IO.FileMode.Append, IO.FileAccess.Write)
    Dim sw As New System.IO.StreamWriter(stream)

    'Here we automatically create a CSV formatted text file
    'which in turn can be used to import into Excel. Cool, huh?
    For Each row As DataGridViewRow In DataGridView1.Rows
        Dim line As String = ""
        line = row.Cells(0).Value.ToString()
        line &= ";" & row.Cells(1).Value.ToString()
        line &= ";" & row.Cells(2).Value.ToString()
        line &= ";" & row.Cells(3).Value.ToString()
        sw.WriteLine(line)
    Next

    sw.Flush()
    sw.Close()
End Sub

okay this is what I did:
Created a button and called it btnSave.
double clicked and went to code (on the general declarations is where it placed my button)
typed all of what you put under that

AND
all went well with no errors until I tested it and tried to save it.
it told me this:
ON the line where it says row.cells(0) it gives me an error stating null refrence exception was unhandled?

and in the save file dialog box (where I can pick what to name file and where to save it) and stuff it
let me name my file
but the where to and what type of file it is..was blued out and would not let me type in the box,
instead it let me leave it blank
so i went to where I did save it to and tried to open it...i got a option box to pick what program to open the file with..so i chose first word pad..it did open word pad but the page was empty
so then i picked excel office..and again it opened excel but the page was empty.

just wondering what I missed, lol...knowing me it sumpin big! hehehehee

You need to add some checking here and there.

Try it like this, add and replace the code in red:

Private Sub SaveButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SaveButton.Click
   Dim fileName As String = ""
   Dim dlgSave As New SaveFileDialog
   dlgSave.Filter = "Text files (*.txt)|*.txt|CSV Files (*.csv)|*.csv"
   dlgSave.AddExtension = True
   dlgSave.DefaultExt = "txt"

   If dlgSave.ShowDialog = Windows.Forms.DialogResult.OK Then
      fileName = dlgSave.FileName
      SaveToFile(fileName)
   End If
End Sub

Private Sub SaveToFile(ByVal fileName As String)
   If DataGridView1.RowCount > 0 AndAlso DataGridView.Rows(0).Cells(0) IsNot Nothing Then
      Dim stream As New System.IO.FileStream(fileName, IO.FileMode.Append, IO.FileAccess.Write)
       Dim sw As New System.IO.StreamWriter(stream)

       'Here we automatically create a CSV formatted text file
       'which in turn can be used to import into Excel. Cool, huh?
       For Each row As DataGridViewRow In DataGridView1.Rows
           Dim arrLine() As String() = New String() {}
           Dim line As String
           row.Cells.CopyTo(arrLine, 0)
           line = arrLine(0)
           line &= ";" & arrLine(1)
           line &= ";" & arrLine(2)
           line &= ";" & arrLine(3)
           ' and so on
           sw.WriteLine(line)
       Next

       sw.Flush()
       sw.Close()
   End If
End Sub

Ahhh man I cant thank you enough for all the patience and understanding you have shown! it is definatly above and beyond :)

Okay..This is what I typed:

Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        Dim fileName As String = ""
        Dim dlgSave As New SaveFileDialog
        dlgSave.Filter = "Text files (*.txt)|*.txt|CSV Files (*.csv)|*.csv"
        dlgSave.AddExtension = True
        dlgSave.DefaultExt = "txt"
        If dlgSave.ShowDialog = Windows.Forms.DialogResult.OK Then
            fileName = dlgSave.FileName
            SaveToFile(fileName)
        End If
    End Sub
    Private Sub SaveToFile(ByVal fileName As String)
        If DataGridView1.RowCount > 0 AndAlso DataGridView1.Rows(0).Cells(0) IsNot Nothing Then
            Dim stream As New System.IO.FileStream(fileName, IO.FileMode.Append, IO.FileAccess.Write)
            Dim sw As New System.IO.StreamWriter(stream)
            For Each row As DataGridViewRow In DataGridView1.Rows
                Dim arrLine() As String = New String() {}
                Dim line As String
                row.Cells.CopyTo(arrLine, 0)
                line = arrLine(0)
                line &= ";" & arrLine(1)
                line &= ";" & arrLine(2)
                line &= ";" & arrLine(3)
                line &= ";" & arrLine(4)
                line &= ";" & arrLine(5)
                line &= ";" & arrLine(6)
                line &= ";" & arrLine(7)
                line &= ";" & arrLine(8)
                line &= ";" & arrLine(9)
                sw.WriteLine(line)
            Next
            sw.Flush()
            sw.Close()
        End If
    End Sub
End Class

And alot of the issues were solved...we are down to 1 :)

It is telling me that the line:

row.cells.copyTo(arrLine,0)

it says that the destination array was not long enough. Check DestIndex and length, and the arrays lower bounds.

I went ahead and typed in the file save box a name for the test file, and for file types it was giving me options of all files and text files- so that was good, and so i chose the text files and it opened up notepad. of course it was blank cuz of the error I figure..but hey, we are getting closer! :)

by the way, dont know if I got the jist of this but the datagridview is 9 columns wide so where you were doing the

line &= ";" & arrLine(1)

I took that for 9 lines.

maybe this would help some..here are my fields:
Date
Employee
Time In
Bay Number
Customer Name
Estimated Proj. Time
Task
Status
Finished Time

And, I would like to tell you my real name is Jenn, and Im so glad to have met you :)

It is telling me that the line:

row.cells.copyTo(arrLine,0)

it says that the destination array was not long enough. Check DestIndex and length, and the arrays lower bounds.

by the way, dont know if I got the jist of this but the datagridview is 9 columns wide so where you were doing the

line &= ";" & arrLine(1)

I took that for 9 lines.

Actually, it's not 9 lines.
As it states in the uppermost codesnippet, it is the 9 cells (columns) of the current row. The array arrLine is just 1 line (row).

And because you know the number of columns beforehand, we can replace the line Dim arrLine() As String = New String() {} with Dim arrLine(9) As String Also, because both the Cells collection and the array is zero-based, you can remove the line line &= ";" & arrLine(9) .
0 to 8 = 9.

I hope that clear things up a bit.

And, I would like to tell you my real name is Jenn, and Im so glad to have met you :)

My name is Tomas. Very nice to meet you. :)

gosh Thomas it seems it just doesnt like this line here:

row.Cells.CopyTo(arrLine, 0)

this time on that line it gives me an error:
Invalid Cast exception was unhandled. At least one element in the source array could not be cast down to the destination array type.

ya know, this debugging error stuff in studio is helpful and all as far as allowing stuff to be caught before sending it out- but it really seems to be lacking LOL..you would think that microsoft could explain these errors in terms where inexperienced people can understand them, instead of being full of yet more jargon! hehehee

anyways, the upside to all of this is that when I went to save the dialog box did give me cvs as an extension...and did open excel :) that tickles me pink! :) lol (almost purple)

okay: here is what I have for code thus far:

Dim fileName As String = ""
        Dim dlgSave As New SaveFileDialog
        dlgSave.Filter = "Text files (*.txt)|*.txt|CSV Files (*.csv)|*.csv"
        dlgSave.AddExtension = True
        dlgSave.DefaultExt = "txt"
        If dlgSave.ShowDialog = Windows.Forms.DialogResult.OK Then
            fileName = dlgSave.FileName
            SaveToFile(fileName)
        End If
    End Sub
    Private Sub SaveToFile(ByVal fileName As String)
        If DataGridView1.RowCount > 0 AndAlso DataGridView1.Rows(0).Cells(0) IsNot Nothing Then
            Dim stream As New System.IO.FileStream(fileName, IO.FileMode.Append, IO.FileAccess.Write)
            Dim sw As New System.IO.StreamWriter(stream)
            For Each row As DataGridViewRow In DataGridView1.Rows
                Dim arrLine(9) As String
                Dim line As String
                [B]row.Cells.CopyTo(arrLine, 0)[/B] 
                line = arrLine(0)
                line &= ";" & arrLine(1)
                line &= ";" & arrLine(2)
                line &= ";" & arrLine(3)
                line &= ";" & arrLine(4)
                line &= ";" & arrLine(5)
                line &= ";" & arrLine(6)
                line &= ";" & arrLine(7)
                line &= ";" & arrLine(8)
                sw.WriteLine(line)
            Next
            sw.Flush()
            sw.Close()

        End If
    End Sub

The bolded line is the line it doesnt seem to like.
Maybe it is having a conflict with the line????

line = arrLine(0)

Just a thought..

Oookay.
The error is because of how the CopyTo method recieves it's arguments, ie the datatypes. The first argument should be an array, but for some reason it won't accept the fact that the variable arrLine is an array.

So let's try this then. See if anyone of these codesnippets works.
Replace the lines in red with the ones in green.

Dim arrLine(9) As String
   Dim arrLines As Array = New String() {}
   Dim line As String
   row.Cells.CopyTo(arrLine, 0)
Dim arrLine(9) As String
   Dim line As String
   row.Cells.CopyTo(arrLine, 0)
   row.Cells.CopyTo(DirectCast(arrLine, Array), 0)
Dim arrLine(9) As String
   Dim arrLines As Array = New String() {}
   Dim line As String
   row.Cells.CopyTo(arrLine, 0)
   row.Cells.CopyTo(DirectCast(arrLine, Array), 0)

There's not just one way to declare and use variables. :)

hello and good morning to ya :)

Okay here are the options back and what happened:

Option 1#

Dim arrLines As Array = New String() {}
                Dim line As String
                row.Cells.CopyTo(arrLine, 0)
                line = arrLine(0)
                line &= ";" & arrLine(1)
                line &= ";" & arrLine(2)
                line &= ";" & arrLine(3)
                                sw.WriteLine(line)
        End If

Gives me error: arrLine is not declared.

Second Option:

Dim arrLine(9) As String
                Dim line As String
                row.Cells.CopyTo(DirectCast(arrLine, Array), 0)
                line = arrLine(0)
                line &= ";" & arrLine(1)
                line &= ";" & arrLine(2)
                 sw.WriteLine(line)

Gives me error :
At least one element in the source array could not be cast down to the destination array type.

Troubleshooting tips:
When casting from a number, the value must be a number less than infinity.

Make sure the source type is convertible to the destination type.

Option #3:

Dim arrLines As Array = New String() {}
                Dim line As String
                row.Cells.CopyTo(DirectCast(arrLine, Array), 0)
                line = arrLine(0)
                line &= ";" & arrLine(1)
                line &= ";" & arrLine(2)

error list tells me: arrLine is not declared.


LOL man o man I really cant thank you enough for all of this as most people would have given up and left me hanging by now!

dunno if you drink, but if you do...I need ta go an pick ya a pallet someday :) for all your help.

AND..when we get this figured out it will stay with me the rest of my life heheheee...sheeeeesh!

Aaaargh! Now I see what's wrong. My bad. :)
Change the name of the variable.
Remove the 's' from arrLines in the declaration.

'Change this
Dim arrLine[B]s[/B] .....

' Into this
Dim arrLine .....

okaaaaaaay :)

I will do that right after i grab a valium LOL

be right back!

Okay...we are gettin there! :)

Heres the code is used:

Private Sub SaveToFile(ByVal fileName As String)
        If DataGridView1.RowCount > 0 AndAlso DataGridView1.Rows(0).Cells(0) IsNot Nothing Then
            Dim stream As New System.IO.FileStream(fileName, IO.FileMode.Append, IO.FileAccess.Write)
            Dim sw As New System.IO.StreamWriter(stream)
            For Each row As DataGridViewRow In DataGridView1.Rows
                Dim arrLine As Array = New String() {}
                Dim line As String
                row.Cells.CopyTo(arrLine, 0)
                line = arrLine(0)
                line &= ";" & arrLine(1)
                line &= ";" & arrLine(2)
                line &= ";" & arrLine(3)

The error occured here:

row.Cells.CopyTo(arrLine, 0)
Gave me this:

Destination array was not long enough. Check destIndex and length, and the array's lower bounds.

this could be another mix up by me cuz maybe with all the options I am mixing options LOL

what would happen if I were to bind the gridview? at the moment it is not bound to anything...

Just a thought cuz it says lower bounds..Im not sure what that means but I see a binding attribute in the property window of the gridview.

Well. Nothing really, except that you would have a data source attached to the DataGrid.
If that is your wish, then you also have to add a bunch of code to deal with updating the datasource with new values.

However, I believe I finally found the solution for the problem at line row.Cells.CopyTo(arrLine, 0) .

They talk about this exact issue in this discussion.
But in your case, if the row currently being read does not contain any information, then the CopyTo method will fail.
Under the line For Each row As DataGridViewRow In DataGridView1.Rows add a line that says If row.Cells(0).Value.Equals("") OrElse row.Cells(0).Value Is Nothing Then Exit For

lol...good morning!

sorry I had to take a powder yesterday. Something came up and I had to make a run. Was gonna let ya know but just didnt have the time to even do that!

As far as the binding, no I dont wanna go that way LOL. it was just a thought and I am having a hard enough time with everything else I certainly dont want to complicate my situation even more.

I also feel your right, the CopyTo line is somehow not completing the task.

I just woke up and am having my coffeee, so my brain is tryin ta catch up heheheheee.

I will for sure try this in just a bit. But something inside me is telling me that this will be the answer :)!

Keeping my fingers crossed. :)

Okay hiya :)

this is what code is at this point:

Private Sub SaveToFile(ByVal fileName As String)
        If DataGridView1.RowCount > 0 AndAlso DataGridView1.Rows(0).Cells(0) IsNot Nothing Then
            Dim stream As New System.IO.FileStream(fileName, IO.FileMode.Append, IO.FileAccess.Write)
            Dim sw As New System.IO.StreamWriter(stream)
            For Each row As DataGridViewRow In DataGridView1.Rows
                If row.Cells(0).Value.Equals("") OrElse row.Cells(0).Value Is Nothing Then Exit For
                Dim arrLine As Array = New String() {}
                Dim line As String
                row.Cells.CopyTo(arrLine, 0)
                line = arrLine(0)
                line &= ";" & arrLine(1)
                line &= ";" & arrLine(2)
                line &= ";" & arrLine(3)
                line &= ";" & arrLine(4)

It is still giving me the same error code on the row.Cells.CopyTo(arrLine,0)

Destination array was not long enough. Check destIndex and length, and the array's lower bounds.

For some reason it just does not like this line...LOL...too bad we cant just erase it heheheee

Actually. We can remove it and go another way.
I was just being stubborn, thinking I'd make it work.

Private Sub SaveToFile(ByVal fileName As String)
        If DataGridView1.RowCount > 0 AndAlso DataGridView1.Rows(0).Cells(0) IsNot Nothing Then
            Dim stream As New System.IO.FileStream(fileName, IO.FileMode.Append, IO.FileAccess.Write)
            Dim sw As New System.IO.StreamWriter(stream)
            For Each row As DataGridViewRow In DataGridView1.Rows
                Dim line As String
                line = row.Cells(0).ToString()
                line &= ";" & row.Cells(1).ToString()
                line &= ";" & row.Cells(2).ToString()
                line &= ";" & row.Cells(3).ToString()
                line &= ";" & row.Cells(4).ToString()
                line &= ";" & row.Cells(5).ToString()
                line &= ";" & row.Cells(6).ToString()
                line &= ";" & row.Cells(7).ToString()
                line &= ";" & row.Cells(8).ToString()
            Next

Okay :)

this has got to be an easy fix here.

On this line:

If DataGridView1.RowCount > 0 AndAlso DataGridView1.Rows Then 0).Cells(0) IsNot Nothing Then

It gives me 2 small stupid errrors LOL

1. Operator 'And' is not defined for types 'Boolean' and 'System.Windows.Forms.DataGridViewRowCollection'.

and the other error on that line is:
Syntax Error ( I kinda know now through all of this that it doesnt like the word IF at the beginning of the line?)

And by the way, Gooood Morning :)

Ooooo! okay...thats not where the syntax error is. The syntax error is here:
Then 0).Cells(0) IsNot Nothing Then (it is under the Then 0). Cells...it doesnt like the first 0.

And the Operator error is under the
DataGridView1.RowCount > 0 AndAlso DataGridView1.Rows part of the line.

No, it doesn't.
Try this instead:

If DataGridView1.RowCount > 0 AndAlso DataGridView1.Rows(0).Cells(0) IsNot Nothing Then

Click the link (Toggle Plain Text) above and then copy the line.

okay now it tells me that 'If' must be matching with a 'End If'
so I put 'End If" after then and it tells me that 'End If has to be preceeded with a matching 'If'

put 'If' on the beginning of this line:
Dim stream As New System.IO.FileStream(fileName, IO.FileMode.Append, IO.FileAccess.Write)

gives me 4 different errors

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.