Managing bound datagridview with a class?

时间:2018-01-23 19:15:45

标签: vb.net

Sorry for the formulation, It's the first time thas I post a question here.

I would like to manage the datagridview + bindingnavigator with a class + implement textbox below the spread with values of the line selected. This datagridview is bound with database's table. I can fill the spread and implement textbox.

But, when I move in the spread with the arrows of the bindingnvigator, the contents of textbox are always late of a line with regard to the active line. When I click in the spread and after I use the arrows, the synchronization is lost.

It's not easy to show all the code. There are many events.

Could you help me ? Many thanks ! Luc-Emmanuel

My class:

Imports System.Data.OleDb

Public Class clsSpread
    Private dgv As DataGridView
    Private da As OleDbDataAdapter
    Private bs As BindingSource
    Private bn As BindingNavigator
    Private fconn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Alltraces_Network\Data\Alltraces_local_EDI.accdb")

Public Sub New(pDgv As DataGridView, pBn As BindingNavigator)

    dgv = pDgv
    bn = pBn
    bs = New BindingSource

End Sub

Public Sub pRefresh(pStrSql As String)

    Dim da As New OleDbDataAdapter(pStrSql, fconn)
    Dim dt As New DataTable(pStrSql)

    da.Fill(dt)
    dgv.DataSource = dt

    bs.DataSource = dt
    bn.BindingSource = bs

End Sub

Public Sub bs_PositionChanged(pPosition As Integer)
    If pPosition < 0 Then Exit Sub
    dgv.CurrentCell = dgv(dgv.CurrentCell.ColumnIndex, pPosition)      'provoque l'event dgvCde_SelectionChanged

End Sub

On the form size:

Public Class Form1
    Dim objdgvCde As clsSpread
    Dim objdgvCde_Item As clsSpread
 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    strSql = "select idClient, idCde_Int, idCde_Client, XDock from qryCde_Client order by idClient"
    objdgvCde = New clsSpread(dgvCde, bnCde_Client)
    objdgvCde.pRefresh(strSql)

end sub

Private Sub dgvCde_CurrentCellChanged(sender As Object, e As EventArgs) Handles dgvCde.CurrentCellChanged
    Dim intIdx%
    Dim strSql$
    Dim dgvRow As DataGridViewRow

    intIdx = dgvCde.CurrentRow.Index
    dgvRow = dgvCde.Rows(intIdx)

    txtIdCde_Int2.Text = dgvRow.Cells("idCde_Int").Value
end sub

Private Sub BindingNavigatorMoveNextItem1_Click(sender As Object, e As EventArgs) Handles BindingNavigatorMoveNextItem1.Click
    'move next
    Dim intIdx%
    intIdx = objdgvCde.Get_Row_Index
    objdgvCde.bs_PositionChanged(intIdx + 1)

End Sub

end class

0 个答案:

没有答案