如何保存更改?

时间:2019-04-05 13:23:28

标签: database vb.net winforms datagridview save

在我的代码中,所有功能都能正常工作:保存按钮,更新和删除实际上可以正常工作,但是当我关闭程序并重新打开它时,所有更改都消失了。
因此,Save按钮仅在我打开程序时有效;我希望当我更改任何内容时,它的代码也会更改。

顺便说一句,数据包含在DataGridView中。

Imports System.Data.DataTable

Public Class Form1
    Dim table As New DataTable("Table")
    Dim index As Integer

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        table.Columns.Add("ID", Type.GetType("System.Int32"))
        table.Columns.Add("First Name", Type.GetType("System.String"))
        table.Columns.Add("Last Name", Type.GetType("System.String"))
        table.Columns.Add("Age", Type.GetType("System.Int32"))
        table.Columns.Add("Programming", Type.GetType("System.Int32"))
        table.Columns.Add("Law", Type.GetType("System.Int32"))
        table.Columns.Add("English", Type.GetType("System.Int32"))
        table.Columns.Add("GPA", Type.GetType("System.Int32"))
        table.Columns.Add("Grade", Type.GetType("System.string"))

        table.Rows.Add(1, "XXXX", "YYYYY", 21, 88, 77, 90, 89)
        table.Rows.Add(2, "SSDD", "hGSQ", 33, 70, 96, 72, 82)
        table.Rows.Add(3, "fgfgd", "jgfdd", 53)
        table.Rows.Add(4, "cvfghyghj", "sdrgtyh", 19)
        table.Rows.Add(5, "hghfd", "ghjgdf", 36)
        table.Rows.Add(6, "cvvdfgh", "juyrfdvc", 63)
        table.Rows.Add(7, "aefht", "cvfhytrff", 21)
        table.Rows.Add(8, "wghyuj", "mihgdwrh", 33)
        table.Rows.Add(9, "qsztii", "bvdhjh", 53)
        table.Rows.Add(10, "rytyufd", "esdfzr", 19)

        DataGridView1.DataSource = table
    End Sub

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        Table.rows.Add(TextBoxID.Text, TextBoxFN.Text, TextBoxLN.Text, TextBoxAGE.Text)
        DataGridView1.DataSource = table
    End Sub

    Private Sub TextBox4_TextChanged(sender As Object, e As EventArgs) Handles TextBoxAGE.TextChanged

    End Sub

    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick, DataGridView1.CellClick
        index = e.RowIndex
        Dim selectedRow As DataGridViewRow
        selectedRow = DataGridView1.Rows(index)
        TextBoxID.Text = selectedRow.Cells(0).Value.ToString()
        TextBoxFN.Text = selectedRow.Cells(1).Value.ToString()
        TextBoxLN.Text = selectedRow.Cells(2).Value.ToString()
        TextBoxAGE.Text = selectedRow.Cells(3).Value.ToString()
        TextBoxPro.Text = selectedRow.Cells(4).Value.ToString()
        TextBoxLaw.Text = selectedRow.Cells(5).Value.ToString()
        TextBoxENGL.Text = selectedRow.Cells(6).Value.ToString()
        TextBoxGPA.Text = selectedRow.Cells(7).Value.ToString()
        TextBoxGrade.Text = selectedRow.Cells(8).Value.ToString()
    End Sub

    Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
        Dim newDataRow As DataGridViewRow

        newDataRow = DataGridView1.Rows(index)

        newDataRow.Cells(0).Value = TextBoxID.Text
        newDataRow.Cells(1).Value = TextBoxFN.Text
        newDataRow.Cells(2).Value = TextBoxLN.Text
        newDataRow.Cells(3).Value = TextBoxAGE.Text
    End Sub

    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        DataGridView1.Rows.RemoveAt(index)
    End Sub
End Class

0 个答案:

没有答案