How to save data from datagridview?

lokiram_32

New member
Joined
Apr 17, 2012
Messages
1
Programming Experience
Beginner
This is my code:
Imports MySql.Data.MySqlClient


Public Class Form4


    Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'RamDataSet8.subject' table. You can move, or remove it, as needed.
        'Me.SubjectTableAdapter1.Fill(Me.RamDataSet8.subject)
        'TODO: This line of code loads data into the 'RamDataSet7.registration' table. You can move, or remove it, as needed.
        Me.RegistrationTableAdapter1.Fill(Me.RamDataSet7.registration)
        'TODO: This line of code loads data into the 'RamDataSet4.subject' table. You can move, or remove it, as needed.
        'Me.SubjectTableAdapter.Fill(Me.RamDataSet4.subject)
        'TODO: This line of code loads data into the 'RamDataSet3.course' table. You can move, or remove it, as needed.
        Me.CourseTableAdapter.Fill(Me.RamDataSet3.course)
        'TODO: This line of code loads data into the 'RamDataSet2.registration' table. You can move, or remove it, as needed.
        'Me.RegistrationTableAdapter.Fill(Me.RamDataSet2.registration)


    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim con As New MySqlConnection
        Dim cmd As New MySqlCommand
        Dim Lastname, subj_code, subj_desc, subj_unit, subj_flag As String
        Dim ds As New DataSet
        Dim da As New MySqlDataAdapter
        Dim i As Integer = 0
        con.ConnectionString = "server=localhost;username='root';pwd='root';database=ram"


        con.Open()
        For i = 0 To DataGridView1.RowCount - 1
            Lastname = DataGridView1.Item(0, i).Value.ToString
            subj_code = DataGridView1.Item(1, i).Value.ToString
            subj_desc = DataGridView1.Item(2, i).Value.ToString
            subj_unit = DataGridView1.Item(3, i).Value.ToString
            subj_flag = DataGridView1.Item(4, i).Value.ToString
            da.SelectCommand = cmd
            da.SelectCommand.Connection = con
            da.Fill(ds)
            With cmd
                .CommandType = CommandType.Text
                .CommandText = "INSERT INTO student_subject VALUES(" + addQoute(Lastname) + ", " + _
                          addQoute(subj_code) + ", " + _
                          addQoute(subj_desc) + ", " + _
                          addQoute(subj_unit) + ", " + _
                          addQoute(subj_flag) + ")"
                .Connection = con
                cmd = New MySqlCommand(.CommandText, con)
            End With
        Next
        If (MessageBox.Show("Do you want to continue?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes) Then
            cmd.ExecuteNonQuery()
            MessageBox.Show("Record has been saved.", "Message", MessageBoxButtons.OK)
        Else
            MessageBox.Show("Cancelled.", "Message", MessageBoxButtons.OK)
        End If
        con.Close()


        ComboBox1.Text = ""
        ComboBox2.Text = ""
    End Sub
    Public Function addQoute(ByVal str As String) As String
        str = Trim(str)
        Return "'" + str + "'"
    End Function


    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
        Dim con As New MySqlConnection
        con.ConnectionString = "server=localhost;username='root';pwd='root';database=ram"


        con.Open()
        Dim da As New MySqlDataAdapter("select subj_id, subj_code, subj_desc, subj_flag, subj_unit from subject where subj_id = '" & ComboBox2.SelectedValue.ToString & "'", con)
        Dim dt As New DataTable
        da.Fill(dt)


        Try
            With DataGridView1
                .DataSource = dt
                .Columns(1).Width = 70
            End With
        Catch ex As Exception
            MessageBox.Show("Error.")
        End Try
    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
        Form2.Show()
    End Sub


    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick


    End Sub


    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged


    End Sub
End Class
 
Last edited by a moderator:
I have added code formatting tags to make your code snippet readable. Please do so for us in future. Also, please only post relevant code. If you have empty event handlers then they are obviously irrelevant and have no business being posted. They simply distract us and make it harder to focus on the actual issue.

If you want retrieve, edit and save data from a database then you use a data adapter and a DataTable. You call Fill on the adapter to populate the DataTable and you call Update to save the changes. How you edit the data in between is irrelevant to the retrieving and saving. You may like to check out this thread for more on that part.

Retrieving and Saving Data in Databases

If you want to display data in a DataGridView then you assign the list containing the data to its DataSource property. In your case, that list would be the DataTable. It's generally preferable to use a BindingSource in between though, so bind the DataTable to the BindingSource and the BindingSource to the grid. All transfer of data back and forth is then done for you.
 

Latest posts

Back
Top