vb.net - 在 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 没有显示。
解决方案
代替
form_admin.DataGridView1.Refresh()
尝试
form_admin.DataGridView1.Datasouce = Nothing
form_admin.DataGridView1.DataSource = dtTemp
.Refresh 仅重绘网格,不会触及其数据。
推荐阅读
- oracle - ORA-00933: SQL 命令未正确结束 00933. 00000 - “SQL 命令未正确结束
- user-interface - 是否可以使 Notepad++ 的 UI 选项卡宽度保持不变,而不是响应文件名?(不是制表符)
- python - Django模板变量未加载
- angular - 如何使用预选值初始化下拉列表?
- javascript - 使用 setTimeout() 对较长(约 30 分钟)间隔的性能考虑
- python - js后端无法点击下载链接
- javascript - 为什么笔画颜色没有变化?
- python - 如何通过为每个单独的列列出分组后的所有数据并求和
- c++ - 为什么在整数变量中输入字符会在 C++ 中返回 0?
- c++ - 如何从 C++ 中的列表/数组中随机选择一个数字?