在我的应用程序中,即时消息使用单独的表单添加记录。添加记录后,主页将得到更新,并在第一行显示最新添加的内容。但是在我的编码中,这没有发生。 仅当我移至下一页并返回首页时,最新记录才显示在顶部。 加载主页(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不会显示该内容。
答案 0 :(得分:0)
代替
<YourToolbarView>
<ActivityIndicator IsRunning="IsBusy" IsVisible="IsBusy"/>
<SomeOtherViewsInYourToolBar/>
</YourToolbarView>
尝试
form_admin.DataGridView1.Refresh()
。刷新只会重新绘制网格,而不会触及其数据。