Click here to Skip to main content
16,014,707 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using VB.Net

I have a form containing some text boxes for displaying data from a database (created using MS-ACCESS). One of the table is named as pmast. The form has some buttons which when clicked moves the record up, down, top & bottom. Also it has a button which when clicked opens a datagrid and displays the records. I have written a code to select a particular record from the grid and displays the contents into the form.

Now when I am selecting a particular record from the grid by clicking, the record is changing its sequence and I am not able to move to previous/next record. Pl. suggest a suitable remedy.

The code is as follows :

***** FOR OPENING THE TABLE
VB
Private Sub pmastfrm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.MdiParent = mainform
        ds = "Provider=Microsoft.Jet.OLEDB.4.0;"
        ds = ds & "Data Source=" & Application.StartupPath & "\TCPL.mdb;"
        ds = ds & "Mode=ReadWrite|Share Deny None;"
        ds = ds & "Persist Security Info=False;"
        Dim strcon As New OleDb.OleDbConnection(ds)
        bindda = New OleDb.OleDbDataAdapter("select * from pmast order by rcpno", strcon)
        If Not oleds.Tables("pmast") Is Nothing Then
            oleds.Tables("pmast").Clear()
        End If
        bindda.Fill(oleds, "pmast")
        bindmg = Me.BindingContext(Me.oleds.Tables("pmast"))
        If Me.labrefid.DataBindings.Count > 0 Then
            Me.labrefid.DataBindings.Remove(labrefid.DataBindings("text"))
            Me.labrefid.DataBindings.Add("text", oleds.Tables("pmast"), "rcpno")
        Else
            Me.labrefid.DataBindings.Add("text", oleds.Tables("pmast"), "rcpno")
        End If
        If Me.labrefdt.DataBindings.Count > 0 Then
            Me.labrefdt.DataBindings.Remove(labrefdt.DataBindings("text"))
            Me.labrefdt.DataBindings.Add("text", oleds.Tables("pmast"), "rcpdt", True)
        Else
            Me.labrefdt.DataBindings.Add("text", oleds.Tables("pmast"), "rcpdt", True)
        End If
        If Me.patname.DataBindings.Count > 0 Then
            Me.patname.DataBindings.Remove(patname.DataBindings("text"))
            Me.patname.DataBindings.Add("text", oleds.Tables("pmast"), "pname")
        Else
            Me.patname.DataBindings.Add("text", oleds.Tables("pmast"), "pname")
        End If
        If Me.patadd.DataBindings.Count > 0 Then
            Me.patadd.DataBindings.Remove(patadd.DataBindings("text"))
            Me.patadd.DataBindings.Add("text", oleds.Tables("pmast"), "address")
        Else
            Me.patadd.DataBindings.Add("text", oleds.Tables("pmast"), "address")
        End If
        If Me.patcontact.DataBindings.Count > 0 Then
            Me.patcontact.DataBindings.Remove(patcontact.DataBindings("text"))
            Me.patcontact.DataBindings.Add("text", oleds.Tables("pmast"), "contact")
        Else
            Me.patcontact.DataBindings.Add("text", oleds.Tables("pmast"), "contact")
        End If
        If Me.patsex.DataBindings.Count > 0 Then
            Me.patsex.DataBindings.Remove(patsex.DataBindings("text"))
            Me.patsex.DataBindings.Add("text", oleds.Tables("pmast"), "category")
        Else
            Me.patsex.DataBindings.Add("text", oleds.Tables("pmast"), "category")
        End If
        If Me.patreference.DataBindings.Count > 0 Then
            Me.patreference.DataBindings.Remove(patreference.DataBindings("text"))
            Me.patreference.DataBindings.Add("text", oleds.Tables("pmast"), "reference")
        Else
            Me.patreference.DataBindings.Add("text", oleds.Tables("pmast"), "reference")
        End If
        bindmg.Position = 0
        GroupBox1.Enabled = True
        labrefid.Enabled = False
        labrefdt.Enabled = False
        GroupBox2.Enabled = True
        patname.Enabled = False
        patadd.Enabled = False
        patcontact.Enabled = False
        patsex.Enabled = False
        patreference.Enabled = False
        patage.Enabled = False
    End Sub


***** FOR MOVING THE RECORD TO PREVIOUS / NEXT
VB
Private Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
        If bindmg.Position = 0 Then
            MsgBox("You Are Currently On The First Record", MsgBoxStyle.Information)
        Else
            bindmg.Position = bindmg.Position - 1
        End If
    End Sub

    Private Sub Button5_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button5.Click
        If bindmg.Position = bindmg.Count - 1 Then
            MsgBox("You Are Currently On The Last Record", MsgBoxStyle.Information)
        Else
            bindmg.Position += 1
        End If
    End Sub


***** FOR OPENING THE GRID
VB
Private Sub pmastview_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       Me.PMASTTableAdapter.Fill(Me.TCPLDataSet.PMAST)
       Me.MdiParent = mainform
   End Sub


***** FOR SELECTING A PARTICULAR RECORD FROM GRID
VB
Private Sub PMASTDATA_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles PMASTDATA.CellDoubleClick
        pmastfrm.labrefid.Text = PMASTDATA.CurrentRow.Cells(0).Value
        pmastfrm.labrefdt.Text = PMASTDATA.CurrentRow.Cells(1).Value
        pmastfrm.patname.Text = PMASTDATA.CurrentRow.Cells(2).Value
        pmastfrm.patadd.Text = PMASTDATA.CurrentRow.Cells(3).Value
        pmastfrm.patcontact.Text = PMASTDATA.CurrentRow.Cells(4).Value.ToString
        pmastfrm.patage.Text = PMASTDATA.CurrentRow.Cells(5).Value.ToString
        pmastfrm.patsex.Text = PMASTDATA.CurrentRow.Cells(6).Value.ToString
        pmastfrm.patreference.Text = PMASTDATA.CurrentRow.Cells(7).Value.ToString
        pmastfrm.Show()
        pmastfrm.viewbtn.Enabled = True
        Me.Close()
        Me.Hide()
    End Sub


Now on executing the code, I am not able to navigate between the records. Pl. try to solve this problem....
Posted
Updated 17-Jul-10 17:00pm
v2

1 solution

it would be better if you can post your code..though you can use the rowdatabound() and rowitemcommand() method of gridview to catch the data by its id.

you can then edit it, delete it anything you want and you won't lose sequence of the data also..

hope this ll help you.

try searching on msdn for rowdatabound and rowitemcommand method of gridview.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900