通过datagridview移动时从记录中检索当前数据库值 [英] Retrieving current database values from record while moving thru datagridview

查看:64
本文介绍了通过datagridview移动时从记录中检索当前数据库值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个关于从数据库中获取当前数据的问题。

我的程序是多用户的。

当用户浏览te datagridview时,当前所选记录会从数据库中刷新(以确保您有最新的更改),到目前为止一直很好。



我在datagridview的de row_enter事件中执行此操作。

例如更改文章名称时,我得到dereal值,使用LoadDataRow我做了更新对于数据表,当发生这种情况时,row_enter事件再次触发。 (我不想要的东西)



当在源数据库中删除当前记录时,应该在本地数据表中删除该记录。

这与我在de row_enter事件中执行的功能相同。

当我的查询返回空时我知道记录不再存在。

我做的很简单Table.Rows.Remove因为我不希望我的数据表在这种情况下发现更改。

在这种情况下,row_enter事件再次触发。

这意味着例如选择记录1并在源DB中删除记录2,3,4,移动到记录2(双击)这一个在datagridview中被删除,自动记录3也被删除,记录4现在被选中而不是删除(它应该)



我在错误的地方这样做,因为它不适合我吗?

或这不是正确的事吗?

我希望有人可以帮助我



 私有  Sub  QueryGrid_RowEnter(sender  As  对象,e  As  System.Windows.Forms.DataGridViewCellEventArgs)句柄  .RowEnter 
如果 e.RowIndex> = 0 AndAlso e.ColumnIndex> = 0 AndAlso e.RowIndex< ._ DS.Tables(MainTable).Rows.Count 然后
_BS.CurrencyManager.Position = e.RowIndex
Dim lnKey As 整数 = CType (_ BS.Current,DataRowView).Row.Item( IREC_ID
GetDataBaseValues(lnKey)
结束 如果
结束 Sub





 私人  Sub  GetDataBaseValues( ByVal  tiKey 作为 整数
.Update MainData(tiKey)
' Me.Refresh()
结束 Sub





< pre lang =vb> 公共 Sub UpdateMainData( ByVal tiKey 作为 整数
Dim lcSql As String = String .Format( SELECT * FROM {0} WHERE IREC_ID = {1} Me .MainTable,tiKey)
Dim loTable As DataTable

_DA = Ne w OleDb.OleDbDataAdapter(lcSql,_ Connect)
_DA.Fill(loTable)

如果 loTable.Rows。计数> 0 然后
CType ( _BS.DataSource,DataTable).BeginLoadData()
CType (_ BS.DataSource,DataTable).LoadDataRow(loTable.Rows( 0 )。ItemArray(), True
CType ( _BS.DataSource,DataTable).EndLoadData()
Else
_DS.Tables(MainTable).Rows.Remove( CType (_ BS.Current,DataRowView).Row)
结束 如果

结束 Sub

解决方案

大家好。

与此同时,我经过几个小时的调试后发现了行中不适用于此类操作。

最好使用选择更改事件,然后检查记录是否仍然存在。

如果是,则更新当前记录,如果它没有选择以前的记录,并删除用户想要选择的记录。



我已经为任何人发布了我的解决方案有同样的问题或谁可以使用。



感谢大家花时间阅读我的问题!



 私有  Sub  QueryGrid_SelectionChanged(sender 作为 对象,e 作为 System.EventArgs)句柄  .SelectionChanged 
Dim lnKey 作为 整数 = 0
Dim loFindRow As DataRow

如果 ._ BS.Position> = 0 AndAlso Me ._ BS.Position<> .PreviousPosition OrElse Me ._ BS。 Position = 0 AndAlso Me .PreviousPosition = 0 AndAlso Me .RowCount = 2 然后
如果 IsNothing( CType (_ BS.Current,DataRowView))然后
lnKey =( CType (_ BS.Current,DataRowView).Row.Item( IREC_ID))
否则
退出 Sub
结束 如果
如果 lnKey> 0 然后
如果 GetRecord (lnKey)= 0 然后
如果 Me ._ BS.Position = 0 AndAlso .PreviousPosition = 0 AndAlso .RowCount = 2 然后
其他
._ BS.Position = PreviousPosition
Me .ClearSelection( Me .CurrentCell.ColumnIndex,PreviousPosition, True
结束 如果

loFindRow = ._ DS。表(MainTable).Rows.Find(lnKey)
如果 IsNothing(loFindRow)< span class =code-keyword>然后
._ DS.Tables(MainTable).Rows.Remove(loFindRow)
结束 如果

结束 如果

结束 如果
结束 如果

<跨班=code-keyword>结束 Sub





 私有 功能 GetRecord( ByVal  tiKey  As   Integer  As  整数 
返回 .UpdateMainData(tiKey)
结束 功能





 公共 功能 UpdateMainData(ByVal tiKey  As 整数) As 整数
Dim lcSql As String = String.Format( SELECT * F ROM {0} WHERE IREC_ID = {1},Me.MainTable,tiKey)
Dim loTable As 新DataTable

_DA =新OleDb.OleDbDataAdapter(lcSql,_ Connect)
_DA.Fill(loTable)

如果 loTable.Rows .Count> 0 然后
Me._DS.Tables(MainTable).BeginLoadData()
Me._DS.Tables(MainTable).LoadDataRow(loTable.Rows( 0 )。ItemArray(),True)
Me._DS.Tables(MainTable) .EndLoadData()
结束 如果

返回 loTable.Rows.Count
结束 功能


Hi Guys,

I have a question about getting the current data from the database.
My program is multiuser.
When a user navigates through te datagridview, the current selected record is refreshed from the DB (to be sure that you have the latest changes), so far so good.

I''m doing this all in de row_enter event from datagridview.
When for example an articlename is changed, I get de "real" value, with LoadDataRow I do an update to the datatable, when this is happening, the row_enter event is firing again. (what i don''t want)

When the current record is deleted in the source DB, the record should be deleted in local datatable.
This goes with the same function I do in de row_enter event.
When my query returns empty I know that the record no longer exists.
I do a simple Table.Rows.Remove because I don´t want my datatable finds changes in this case.
Also in this case the row_enter event is firing again.
This means for example record 1 is selected and record 2,3,4 are deleted in the source DB, your moving to record 2 (with double firing) this one is been deleted in datagridview, automatically record 3 is also deleted and record 4 is now selected and not deleted(and it should)

Am i doing this in the wrong place, because it''s not working for me?
Or is this not the right thing to do?
I hope there is somebody who can help me

Private Sub QueryGrid_RowEnter(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Me.RowEnter
         If e.RowIndex >= 0 AndAlso e.ColumnIndex >= 0 AndAlso e.RowIndex < Me._DS.Tables(MainTable).Rows.Count Then
            _BS.CurrencyManager.Position = e.RowIndex
            Dim lnKey As Integer = CType(_BS.Current, DataRowView).Row.Item("IREC_ID")
            GetDataBaseValues(lnKey)
         End If
      End Sub



Private Sub GetDataBaseValues(ByVal tiKey As Integer)
   Me.UpdateMainData(tiKey)
   'Me.Refresh()
End Sub



Public Sub UpdateMainData(ByVal tiKey As Integer)
         Dim lcSql As String = String.Format("SELECT * FROM {0} WHERE IREC_ID = {1}", Me.MainTable, tiKey)
         Dim loTable As New DataTable

         _DA = New OleDb.OleDbDataAdapter(lcSql, _Connection)
         _DA.Fill(loTable)

         If loTable.Rows.Count > 0 Then
            CType(_BS.DataSource, DataTable).BeginLoadData()
            CType(_BS.DataSource, DataTable).LoadDataRow(loTable.Rows(0).ItemArray(), True)
            CType(_BS.DataSource, DataTable).EndLoadData()
         Else
            _DS.Tables(MainTable).Rows.Remove(CType(_BS.Current, DataRowView).Row)
         End If

      End Sub

解决方案

Hi everybody.
In the meantime, I have after many hours and lots of debugging found that the rowenter does not work for this type of operations.
It''s better to use the selection change event, then check if the record still exists.
If is does, then update the current record, if it doesn''t select the previous record, and delete the record that the user wants to select.

I have posted my solution for anyone who has the same problem or who can use.

Thanks to everyone who took the time to read my question!

Private Sub QueryGrid_SelectionChanged(sender As Object, e As System.EventArgs) Handles Me.SelectionChanged
         Dim lnKey As Integer = 0
         Dim loFindRow As DataRow

         If Me._BS.Position >= 0 AndAlso Me._BS.Position <> Me.PreviousPosition OrElse Me._BS.Position = 0 AndAlso Me.PreviousPosition = 0 AndAlso Me.RowCount = 2 Then
            If Not IsNothing(CType(_BS.Current, DataRowView)) Then
               lnKey = (CType(_BS.Current, DataRowView).Row.Item("IREC_ID"))
            Else
               Exit Sub
            End If
            If lnKey > 0 Then
               If GetRecord(lnKey) = 0 Then
                  If Me._BS.Position = 0 AndAlso Me.PreviousPosition = 0 AndAlso Me.RowCount = 2 Then
                  Else
                     Me._BS.Position = PreviousPosition
                     Me.ClearSelection(Me.CurrentCell.ColumnIndex, PreviousPosition, True)
                  End If
                  '
                  loFindRow = Me._DS.Tables(MainTable).Rows.Find(lnKey)
                  If Not IsNothing(loFindRow) Then
                     Me._DS.Tables(MainTable).Rows.Remove(loFindRow)
                  End If

               End If

            End If
         End If

      End Sub



Private Function GetRecord(ByVal tiKey As Integer) As Integer
   Return Me.UpdateMainData(tiKey)
End Function



Public Function UpdateMainData(ByVal tiKey As Integer) As Integer
   Dim lcSql As String = String.Format("SELECT * FROM {0} WHERE IREC_ID = {1}", Me.MainTable, tiKey)
   Dim loTable As New DataTable

   _DA = New OleDb.OleDbDataAdapter(lcSql, _Connection)
   _DA.Fill(loTable)

   If loTable.Rows.Count > 0 Then
      Me._DS.Tables(MainTable).BeginLoadData()
      Me._DS.Tables(MainTable).LoadDataRow(loTable.Rows(0).ItemArray(), True)
      Me._DS.Tables(MainTable).EndLoadData()
   End If

   Return loTable.Rows.Count
End Function


这篇关于通过datagridview移动时从记录中检索当前数据库值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆