2012-02-23 91 views
0

这看起来像一个简单的修复,但我无法弄清楚。我试图在subForm(NewTournament)上添加按钮单击事件,向数据库添加一条记录,然后通知一个数据网格,该数据网格自动列出来自同一数据库的记录(网格在HomeForm中列出)。刷新数据网格视图

我很高兴能够更新数据库并调用新窗口。但我无法让它刷新该数据网格的任何东西。我知道我应该清除数据网格,获取更改,然后重新填充网格。但每次我这样做,代码都不会更新。此外,我可以很好地看到记录正在被添加。

Private Sub CreateTournament_Click(sender As System.Object, e As System.EventArgs) Handles CreateTournament.Click 
    ' Check the form for errors, if none exist. 
    ' Create the tournament in the database, add the values where needed. Close the form when done. 

    Dim cn As OleDbConnection 
    Dim cmd, cmd1 As OleDbCommand 
    Dim icount As Integer 
    Dim str, str1 As String 

    Try 
     cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='C:\Users\Paul Williams III\Documents\Visual Studio 2010\Projects\Everything OP Client\Everything OP Client\Master.mdb'") 
     cn.Open() 
     str = "insert into Tournaments (SanctioningID,TournamentName,TournamentVenue,TournamentDateTime,TournamentFirstTable,Game,Format,OrganizerID) values(" _ 
      & CInt(SanctioningIDTxt.Text) & ",'" & Trim(TournamentNameTxt.Text) & "','" & _ 
      "1" & "','" & EventDateTimePck.Value & "','" & TableFirstNumberNo.Value & "','" & GameList.SelectedIndex & "','" & FormatList.SelectedIndex & "','" & Convert.ToInt32(ToIDTxt.Text) & "')" 

     'string stores the command and CInt is used to convert number to string 
     cmd = New OleDbCommand(str, cn) 
     str1 = "select @@Identity" 
     icount = cmd.ExecuteNonQuery 
     cmd1 = New OleDbCommand(str1, cn) 
     Counter = CInt(cmd1.ExecuteScalar) 
     MessageBox.Show(Counter & " was the last inserted id") 
     'displays number of records inserted 

     HomeForm.MasterDataSet.Clear() 
     HomeForm.MasterDataSet.GetChanges() 
     HomeForm.TournamentsTableAdapter.Fill(HomeForm.MasterDataSet.Tournaments) 
     HomeForm.DataGridView1.Refresh() 

    Catch ex As Exception 
     MessageBox.Show(ex.ToString) 
    End Try 

    Me.Close() 

    Dim n As New TournamentWindow 
    n.TournID = Counter 
    n.Show(HomeForm) 

End Sub 

回答

1

最好的方法是将记录添加到DataGrid的DataSource。 DataGrid将自动更新。在这种情况下,其可能是服用点像

HomeForm.MasterDataSet.Tournaments.AddTournamentsRow(CInt(SanctioningIDTxt.Text) , _ 
          Trim(TournamentNameTxt.Text) , _ 
          "1" , _ 
          EventDateTimePck.Value, _ 
          TableFirstNumberNo.Value, _ 
          GameList.SelectedIndex, _ 
          FormatList.SelectedIndex, _ 
          Convert.ToInt32(ToIDTxt.Text)) 

顺便说一句,你可能要考虑使用参数化查询您的Insert语句

+0

当你说参数化查询时,你是什么意思? – 2012-02-24 21:14:46

+1

而不是串联你的值,你把'?'作为占位符,然后设置参数值。这可以解决问题,比如你的比赛名称是“O'Brien's Cup”,同时也消除了潜在的[SQL注入](http://xkcd.com/327/)攻击。这里是c#的Access [示例](http://stackoverflow.com/questions/9401888/parameterized-query-for-inserting-values)。对不起,我找不到任何VB.NET访问,但它并没有完全不同。 – 2012-02-24 21:24:23

+0

当我使用你提供的代码时,我得到一个错误:“重载解析失败,因为没有可访问的'AddTournamentsRow'接受这个数量的参数编辑:我只是回答我自己的问题显然,MasterDataSet没有所有这些参数(还是新的) – 2012-02-24 23:12:16

0

这就是我的答案,我希望你喜欢它。在我的项目中,我有一个配电盘,并在该配电盘中包含数据的数据网格视图。在用户使用查询时,它只能自我刷新,并且会自我刷新。我教过它如何做到与众不同。所以我使用这种技术的定时器刷新效果。它是如何工作的我把一个计时器,并在计时器中复制并粘贴这个代码从表单加载。

Me.ADDRESS_TICKETTableAdapter.Fill(Me.Database1DataSet.ADDRESS_TICKET)

并且在定时器的设定我打开它,并每9秒它重新加载你可以改变它的数据,它重新加载,如果每1秒你喜欢取决于它有多少记录。我希望这个答案无论如何都能帮助你。它适合我。

+0

我需要使用定时器吗 – 2013-08-12 23:51:11

+0

这取决于你想要完成什么保罗我使用了一个定时器在定时器中输入代码,我选择了几秒钟或者几分钟或几小时,我希望它能够刷新,以便我的同事可以看到新的数据 – NOE2270667 2013-08-13 13:18:57