Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > How do I save data entered into multiple rows in a datagridview
 

How do I save data entered into multiple rows in a datagridview

I have a datagridview in which the user can enter data into multiple new rows. What I want the app to do is when the user clicks on the Save button, it will save the multiple new rows and insert the new rows in the database, where the datagridview gets the data from. Thanks in advance.

Here is my code for what I have so far:

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click



        Dim sConnString As String = "Provider=SQLOLEDB;Data Source=tripleX; Database=MyClaims;"

        Dim dgv_sql As String = "INSERT INTO " + ComboBox1.SelectedValue + " (CODE, SDESCR, LDESCR, STARTDT, ENDDT) VALUES (?,?,?,?, ?)"

        Dim connString As New OleDbConnection(sConnString)

        Dim cmd As OleDbCommand = New OleDbCommand(dgv_sql, connString)

        Dim objDataAdapter As New OleDbDataAdapter



        Dim strCode As String

        Dim strSDescr As String

        Dim strLDescr As String

        Dim strStartDate As String

        Dim strEndDate As String

        Dim counter As Integer



        connString.Open()



        For counter = 0 To (dgv_Investigation.Rows.Count - 1)



            strCode = dgv_Investigation.SelectedRows(counter).Cells("CODE").Value

            strSDescr = dgv_Investigation.SelectedRows(counter).Cells("SDESCR").Value

            strLDescr = dgv_Investigation.SelectedRows(counter).Cells("LDESCR").Value

            strStartDate = dgv_Investigation.SelectedRows(counter).Cells("STARTDT").Value

            strEndDate = dgv_Investigation.SelectedRows(counter).Cells("ENDDT").Value



            cmd.ExecuteNonQuery()

            objDataAdapter.InsertCommand = cmd

            cmd.CommandText = "INSERT INTO " + ComboBox1.SelectedValue + " (CODE, SDESCR, LDESCR, STARTDT, ENDDT) VALUES (?,?,?,?,?)"

            cmd.Parameters.Add(strCode)

            cmd.Parameters.Add(strSDescr)

            cmd.Parameters.Add(strLDescr)

            cmd.Parameters.Add(strStartDate)

            cmd.Parameters.Add(strEndDate)



        Next



        connString.Close()


BTW I am using VB.NET 2005
Some programmer  Wednesday, June 17, 2009 9:47 PM
Hi Some programmer,

Please look at the following sample.
http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/a884fe41-4ab1-4cc1-a7f3-3a31e9b68800

Though it is written in C#, you can convert it to VB using this tool. http://www.developerfusion.com/tools/convert/csharp-to-vb/

In this example, these component is necessary:
OleDbConnection
OleDbCommandBuilder
OleDbDataAdapter
DataSet

OleDbCommandBuilder will generate the insert/update/delete sql for you base on the RowState.

If you have any problem, please feel free to tell me.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Friday, June 19, 2009 9:03 AM
Hi Some programmer,

Please look at the following sample.
http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/a884fe41-4ab1-4cc1-a7f3-3a31e9b68800

Though it is written in C#, you can convert it to VB using this tool. http://www.developerfusion.com/tools/convert/csharp-to-vb/

In this example, these component is necessary:
OleDbConnection
OleDbCommandBuilder
OleDbDataAdapter
DataSet

OleDbCommandBuilder will generate the insert/update/delete sql for you base on the RowState.

If you have any problem, please feel free to tell me.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Friday, June 19, 2009 9:03 AM

You can use google to search for other answers

Custom Search

More Threads

• How to customize DataGridView in DataGridViewCell
• DataGridView DataSourceNullValue proprty not found.
• Tab Control
• Datagridview embedded in a usercontrol loses DefaultCellStyle.BackColor after load
• DataGridView - Displaying data in timetable format, not as format from database
• cascading/dependant combobox in gridview in VB 2005
• Datagrid to Excel
• Data Rowview - Adding new row
• Saving Changes in a DataRow to a SQL DataBase (C#)
• Attribute must be unique