首页 > 解决方案 > 在 Gridview 和 VB.net 中添加记录后如何更新 gridview?

问题描述

在我的应用程序中,我使用单独的表单添加记录。添加记录后,主页将更新并在第一行显示最新添加的内容。但在我的编码中,它没有发生。只有当我移动到下一页并返回第一页时,最新记录才会显示在顶部。加载主页 (form_admin.vb)

Private Sub admin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 


        ' module function connect to dbms
        mod_sqlconnect.connect()

        ds.Clear()
        mod_datagrid.gridview("risk") ' module function to use MySQLAdapter

        Adpt.Fill(ds, "risk")
        'Set the start and max records. 
        pageSize = 20
        maxRec = ds.Tables("risk").Rows.Count
        PageCount = maxRec \ pageSize

        ' Adjust the page number if the last page contains a partial page.
        If (maxRec Mod pageSize) > 0 Then
            PageCount = PageCount + 1
        End If

        currentPage = 1
        recNo = 0

        LoadPage()

    End Sub

    Public Sub LoadPage()
        Dim i As Integer
        Dim startRec As Integer
        Dim endRec As Integer
        Dim dtTemp As DataTable
        Dim dr As DataRow

        dtTemp = ds.Tables("risk").Clone

        If currentPage = PageCount Then
            endRec = maxRec
        Else
            endRec = pageSize * currentPage
        End If

        startRec = recNo

        'Copy the rows from the source table to fill the temporary table.
        For i = startRec To endRec - 1
            dtTemp.ImportRow(ds.Tables("risk").Rows(i))
            recNo = recNo + 1
        Next

        DataGridView1.DataSource = dtTemp


    End Sub

插入表格以添加新记录

Private Sub ADD_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click


        'coding to check if all data is entered ……… hided


            'declarations
            Dim ni_newindex As Integer
            Dim ri_insert As MySqlDataReader
            Dim cmdi_insert As New MySqlCommand


            'adding new risk using insert command
            conn.Open()
            cmdi_insert = New MySqlCommand(String.Format("INSERT INTO risk VALUES ({19},'{0}','{1}','{2}','{3}',{4},'{5}','{6}','{7}','{8}','{9}','{10}',{11},{12},'{13}','{14}',{15},{16},'{17}','{18}', curdate(),NULL );", i1.SelectedItem, i2.Text, i3.SelectedItem, i4.SelectedItem, i5.Text, i6.SelectedItem, i7.SelectedItem, i8.SelectedItem, i9.SelectedItem, i10.SelectedItem, i11.SelectedItem, i12.Text, i13.Text, i14.Text, i15.Text, i16.Value, i17.Text, i18.Text, i19.Text, ni_newindex), conn)

            cmdi_insert.ExecuteNonQuery()
            conn.Close()


            ds.Clear()
            mod_datagrid.gridview("risk")
            Adpt.Fill(ds, "risk")

            form_admin.currentPage = 1
            form_admin.recNo = 0
            form_admin.pageSize = 20

            form_admin.LoadPage()
            form_admin.DataGridView1.Refresh()  'Refreshing gridview is not updating the gridview


            Me.Close()

        End If
    End Sub

但是,如果我通过分页编码移至下一页并返回同一页面,则记录就在那里。

下一页和上一页的编码:

Private Sub btnNextbutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNextbutton.Click     

        currentPage = currentPage + 1

        If currentPage > PageCount Then
            currentPage = PageCount           
        End If

        LoadPage()
    End Sub

    Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click

        If currentPage = PageCount Then
            recNo = pageSize * (currentPage - 2)
        End If

        currentPage = currentPage - 1
        If currentPage < 1 Then
            MessageBox.Show("You are at the First Page!")
            currentPage = 1
            Return
        Else
            recNo = pageSize * (currentPage - 1)
        End If

        LoadPage()   
 End Sub

当我调试时,添加新记录后,当我在这里查看时dtTemp.ImportRow(ds.Tables("risk").Rows(i)),它会在监视窗口中显示最新添加,但 gridview 没有显示。

标签: vb.netdatagridview

解决方案


代替

 form_admin.DataGridView1.Refresh()

尝试

form_admin.DataGridView1.Datasouce = Nothing
form_admin.DataGridView1.DataSource = dtTemp

.Refresh 仅重绘网格,不会触及其数据。


推荐阅读