VB.NET - saving contents of datagridview - Asked By jenn on 28-Apr-10 06:44 PM

okay...sheesh!!
this has been one heck of a nightmare for a first project, and i hate to keep buggin all you but i really am having one heck of a time with this!

I have several issues happening and have been going round and round with them for days...to the point of wanting to throw my computer out in front of a speeding bus! *grrrr*

the most major issue at hand is this:

I have these combo boxes, all set up and displaying to the datagridview.
Now what I want to do is save the Datagridview...any way that i can! LOL...
first of all...I dont know how to do this...through savefiledialog...or a save button. and what extension would I give this type of a file.

I was HOPING to save the contents to a database. So what I did was followed a tutorial on how to set up a database and I have all that working..but ummmm..well, how the heck do I get the contents of the datagridview to save to my database that I created?

This database was made sql..a simple in form database. This whole project is set up for in shop computers. The project is not web based.

And...if I cant get the grid to save to the database...I still dont know how to save the grid to even so much as a text file :(

Thats the first Issue..

The second one is that in my comboboxes (by the way, these comboboxes are not sitting in the gridview..they are sitting along side of it)

but anyways,
my comboboxes are set to allow users to enter their own choices. the problem I am having with this is that this is allowing me to type in Johnathan 10000000000 times and add it to the combo boxes LOL..that cant work, I have to be able to prevent this from happening.

AND..
if I happen to skip filling in a combobox...the stupid computer will pick up the last entry made in that combobox and post it to gridview...heheheheee (can I cry now????!!??)  lol..

Phivos Stylianides replied to jenn on 28-Apr-10 07:38 PM
You should iterate the rows of the datagridview and extract the values for each row.

Assuming your grid looks like this:
CustomerID   CustomerName   CustomerPhone
1             J. Smith       (123)456-7890

Then sample code would be:

For Each row As DataGridViewRow In datagridview1.Rows
    Dim id As Integer = Convert.ToInt32(row("CustomerID").Value)
    Dim name As Integer = row("CustomerName").Value.ToString()
    Dim phone As Integer = row("CustomerPhone").Value.ToString()
    ' Check values if valid
    ' Save each customer row to your db
Next

This tutorial describes the database insert/update/delete procedures for SQL Server including comments.
Phivos Stylianides replied to Phivos Stylianides on 28-Apr-10 07:40 PM
And here is the link i forgot :-)

http://www.daniweb.com/code/snippet216920.html
jenn replied to Phivos Stylianides on 28-Apr-10 10:12 PM
hello and thank you for the response :)

The following is what I typed in and the errors were returned as
Row was underlined and error was given because row has no defined property
and  Convert.ToInt32 was returned as a overload failure :(

I did not use an ID number when setting up my database because she (the user) wants to use Employee as the identifier, is this what maybe making a difference?

or maybe I am not typing this code into the right part of my form?..i just double clicked on the datagridview and typed it where it put me.

anyway...I posted the code I used (and I hope I am not breaking any rules doing this)
so that maybe you can clear some of this up with me?

so far..this has given me the least amount of errors :)

is there maybe something I need to do to the database yet?












Private Sub DataGridView1_CellContentClick_1(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        For Each row As DataGridViewRow In DataGridView1.Rows
            Dim Employee As Integer = Convert.ToInt32 = (row("Employee").Value)
            Dim TimeIn As Integer = row("TimeIn").Value.ToString()
            Dim BayNumber As Integer = row("Bay Number").Value.ToString()
            Dim CustomerName As Integer = row("Customer Name").Value.ToString()
            Dim EstimatedProjTime As Integer = row("Estimated Project Time").Value.ToString()
            Dim Task As Integer = row("Task").Value.ToString()
            Dim Status As Integer = row("Status").Value.ToString()
            Dim FinishedTime As Integer = row("Finished Time").Value.ToString()
  
        Next
Phivos Stylianides replied to jenn on 29-Apr-10 06:08 AM
Here is the code corrected:

For Each row As DataGridViewRow In DataGridView1.Rows
            Dim Employee As Integer = Convert.ToInt32((row.Cells("Employee").Value))
            Dim TimeIn As Integer = row.Cells("TimeIn").Value.ToString()
            Dim BayNumber As Integer = row.Cells("Bay Number").Value.ToString()
            Dim CustomerName As Integer = row.Cells("Customer Name").Value.ToString()
            Dim EstimatedProjTime As Integer = row.Cells("Estimated Project Time").Value.ToString()
            Dim Task As Integer = row.Cells("Task").Value.ToString()
            Dim Status As Integer = row.Cells("Status").Value.ToString()
            Dim FinishedTime As Integer = row.Cells("Finished Time").Value.ToString()
        Next

Now you can go ahead and save the rows to the database!
Phivos Stylianides replied to Phivos Stylianides on 29-Apr-10 06:19 AM
Jenn,

I haven't realized there are more errors since my IDE hasn't underlined the data types used as wrong but you only need the Convert.ToInt32 method for integer numbers. Other examples:

Dates -> Dim returnDate As Date = Convert.ToDateTime(row.Cells("ReturnDate").Value)
Doubles -> Dim price As Double = Convert.ToDouble(row.Cells("Price").Value)
Strings -> Dim name As String = row.Cells("Name").ToString()

Cheers,

Phivos
jenn replied to Phivos Stylianides on 29-Apr-10 04:28 PM
okay so what I should be putting is this:
Dim Employee As Integer = ((row.Cells("Employee").Value))
Dim TimeIn As Integer = row.Cells("TimeIn").Value.ToString()
Dim BayNumber As Integer = row.Cells("Bay Number").Value.ToString()
Dim CustomerName As Integer = row.Cells("Customer Name").Value.ToString()
Dim EstimatedProjTime As Integer = row.Cells("Estimated Project Time").Value.ToString()
Dim Task As Integer = row.Cells("Task").Value.ToString()
Dim Status As Integer = row.Cells("Status").Value.ToString()
Dim FinishedTime As Integer = row.Cells("Finished Time").Value.ToString()
Phivos Stylianides replied to jenn on 29-Apr-10 05:08 PM
It really depends how you want to present the data, but if i were to guess by the column names:

For Each row As DataGridViewRow In DataGridView1.Rows
            Dim Employee As String = (row.Cells("Employee").Value).ToString()
            Dim TimeIn As Date = Convert.ToDateTime(row.Cells("TimeIn").Value)
            Dim BayNumber As Integer = Convert.ToInt32(row.Cells("Bay Number").Value)
            Dim CustomerName As String = row.Cells("Customer Name").Value.ToString()
'If you record the amount of time use TimeSpan, otherwise use Date
            Dim EstimatedProjTime As TimeSpan = CType(row.Cells("Estimated Project Time").Value, TimeSpan)
            Dim Task As String = row.Cells("Task").Value.ToString()
            Dim Status As String = row.Cells("Status").Value.ToString()
            Dim FinishedTime As Date = Convert.ToDateTime(row.Cells("Finished Time").Value)
        Next
al replied to Phivos Stylianides on 30-Apr-10 05:24 PM
One addition.  You may need to check if the row your are processing is a new entry depending on your application needs.  For example,

if  row.IsNewRow....
else
    processing in here
end if

This can keep you from processing the bottom row in the datagrid which "inserts" newlines when data is entered manually in a datagridview.