首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

为啥DataGridView的CurrentCellChanged事件会重复执行两次

2012-08-16 
为什么DataGridView的CurrentCellChanged事件会重复执行两次?Public CN As New OleDbConnection(Provider

为什么DataGridView的CurrentCellChanged事件会重复执行两次?
Public CN As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\Dbase.mdb;Persist Security Info=True;Jet OLEDB:Database Password=123")
  Public ds As DataSet = New DataSet() '所有的数据集


  Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

  DataGridView1.DataSource = BindingSource1
  Dim myCommand As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter("select * from 工作台帐视图", CN)
  myCommand.Fill(ds, "主查询结果")
  BindingSource1.DataSource = ds.Tables("主查询结果")

  End Sub

  Private Sub DataGridView1_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.CurrentCellChanged
  '此过程会被执行两次,换成SelectionChanged事件过程也一样
  Debug.Print(2)
  End Sub



Load过程中,执行到BindingSource1.DataSource = ds.Tables("主查询结果")这句时,CurrentCellChanged过程回反复执行两次,非常奇怪!请高手看下,是什么原因?在线等!

[解决办法]
初始化一次。绑定数据一次。
[解决办法]
绑定前执行一次,绑定后执行一次
[解决办法]
public binded as boolean = false

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

DataGridView1.DataSource = BindingSource1
Dim myCommand As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter("select * from 工作台帐视图", CN)
myCommand.Fill(ds, "主查询结果")
BindingSource1.DataSource = ds.Tables("主查询结果")
binded=true

End Sub

Private Sub DataGridView1_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.CurrentCellChanged
'此过程会被执行两次,换成SelectionChanged事件过程也一样
if binded then
Debug.Print(2)
endif
End Sub

热点排行