Friends I m workin on vb.net ado.net oledb, I have also added datagrid Control to my form

for retriving database from .Mdb file i used following code

'decleared Name Space
Imports System.Data.OleDb


'Dicleared Variabls in Class  Form1 

Dim con As New OleDb.OleDbConnection

    Dim ds As New DataSet
    Dim da As OleDb.OleDbDataAdapter
    Dim sql As String
    Dim inc As Integer
    Dim MaxRows As Integer


'created Navigate Records() 'class
txtSno.Text = ds.Tables("SLAPhonebook").Rows(inc).Item("SNo")
        txtName.Text = ds.Tables("SLAPhonebook").Rows(inc).Item("Name")
        txtLastName.Text = ds.Tables("SLAPhonebook").Rows(inc).Item("LastName")
        txtOrganization.Text = ds.Tables("SLAPhonebook").Rows(inc).Item("Organization")
' and so on...

' on form load () event i put following coding
con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=D:\SLAPhonebook.mdb"
        sql = "Select * From sla"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "SLAPhonebook")
        MaxRows = ds.Tables("SLAPhonebook").Rows.Count
inc = -1


' here i connected all text boxes to '0' row of ds.tables(rows) to show first row, as follows


txtSno.Text = ds.Tables("SLAPhonebook").Rows(0).Item("SNo")
        txtName.Text = ds.Tables("SLAPhonebook").Rows(0).Item("Name")
        txtLastName.Text = ds.Tables("SLAPhonebook").Rows(0).Item("LastName")


'here in formload event i also binded data grid with dataset
DataGrid1.SetDataBinding(ds, "sla")
DataGrid1.DataSource = ds.Tables(0)



'friends every thing is working fine, but when i click to move next record in table, it moves but only in text boxes which i connected with dataset.tables(rows), i want to datagrid to also move to next record as soon as i press next record button

' I used following code for "next Record Button", to move on next record

if inc <> MaxRows - 1 Then
            inc = inc + 1
            NavigateRecords()
        Else
            MsgBox("No More Rows")

        End If

DataGrid1.DataSource = ds.Tables(inc)
DataGrid1.SetDataBinding(ds, "sla")

Please Help My with My Question how to move current record in datagrid, i mean when i press Next record button then datagrid's data (table) also should move to next record


or when i click any record in datagrid, then the current record of dataset also should move to clicked record of datagrid, so that i also may see datagrid's clicked record in textboxes

in short i want to use default feature of vb6's ado control, when we bind datagrid with ado control, it worked autometically, both data grid and adodc wer connected each other at a time, so that moving next record also apears in datagrid. please help me in my question, i m new to .net i need ur help

>showing current record of dataset in datagrid

Use bindingSource method.

As I m new to .net Please Help how to use Binding Source Method. please use Code Example here , will be very much thankful of you

>showing current record of dataset in datagrid

Use bindingSource method.

Have a look,

Public Class Form1
    Dim dt As DataTable
    Dim da As OleDb.OleDbDataAdapter

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dt=new DataTable()
        da = New OleDbDataAdapter("Select * From sla", "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=D:\SLAPhonebook.mdb")
        da.Fill(dt)

        DataGridView1.DataSource = dt
        TextBox1.DataBindings.Add("Text", dt, "SNo")
        TextBox2.DataBindings.Add("Text", dt, "Name")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.BindingContext(dt).Position += 1
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.BindingContext(dt).Position -= 1
    End Sub
End Class

Thanks Dear for a nice Code, U Solved my Big problem, your Code Worked, I also added Move First and Move last Coding in it, and first it was creating problem when i tried to run an other sql qurey on same datagrid and textboxes (connected with datatable) then after Discovering vb syntax i also solved that problem with databinding's Clear method for example txtRemarks.DataBindings.Clear()

Yes I also Discoverd Search record method with datatable

one thing which i cant solve still is how to Add, Delete, Edit and Update record with Data Table method, can you please Help men with this

You Helped me a lot, I would be very much thankfull for this support of add delete edit and update record

Regards
Fahad Memon

Have a look,

I have created simple database project, using vb.net, ado.net, oledb, datatable, datagridview.

My program is working nice, I just need Help with how to add, delete, update and edit records. Can any one please and please help me in this case, or can provide any good tutorial or source code for it please.

I used following code

‘Namespaces
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.Common

‘other declearations in Public class form

Public Class Form1

    '''''
    Inherits System.Windows.Forms.Form
    '''''


    Dim dtBook As DataTable
    Dim daBook As New OleDb.OleDbDataAdapter


    Dim Srch As String
    Dim MaxRowsBook As Integer

    ''''''''''''
    Private DataTable As DataTable
    Private CurrRec As Integer = 0


        "
    'Private insertSelected As Boolean


‘ a function declaration for Connecting textboxes and datagridview to datatable

Public Function ClearBookTable()

        txtBookID.DataBindings.Clear()
        txtNameOfBook.DataBindings.Clear()
        txtCompanyID.DataBindings.Clear()
        txtAuthorName.DataBindings.Clear()

        txtBookID.DataBindings.Add("text", dtBook, "BookID")
        txtNameOfBook.DataBindings.Add("text", dtBook, "NameOfBook")
        txtCompanyID.DataBindings.Add("text", dtBook, "CompanyID")
        txtAuthorName.DataBindings.Add("text", dtBook, "AuthorName")
End Function



‘code for form load event

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim daBook As OleDb.OleDbDataAdapter



        dtBook = New DataTable(1)
        daBook = New OleDbDataAdapter("Select * from Books", "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Display Center Software\DisplayCenterData.mdb")
        daBook.Fill(dtBook)

        DataGridBook.DataSource = dtBook

        txtBookID.DataBindings.Add("text", dtBook, "BookID")
        txtNameOfBook.DataBindings.Add("text", dtBook, "NameOfBook")
        txtCompanyID.DataBindings.Add("text", dtBook, "CompanyID")
        txtAuthorName.DataBindings.Add("text", dtBook, "AuthorName")
End Sub


‘code for previous Button

  Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        Me.BindingContext(dtBook).Position -= 1
        CurrRec -= 1
    End Sub

‘code for next button
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        Me.BindingContext(dtBook).Position += 1
        CurrRec += 1

    End Sub

‘code for first Button
Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
        MaxRowsBook = dtBook.Rows.Count
        Me.BindingContext(dtBook).Position = 0

    End Sub

‘code for last button
Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
        MaxRowsBook = dtBook.Rows.Count
        Me.BindingContext(dtBook).Position = MaxRowsBook

    End Sub

‘code for search button
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click

        Srch = txtSearch.Text
        daBook = New OleDbDataAdapter("Select * From Books where NameOfBook like'" & Srch & "%'", "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Display Center Software\DisplayCenterData.mdb")

        dtBook = New DataTable(2)

        daBook.Fill(dtBook)
        DataGridBook.DataSource = dtBook

        ClearBookTable()

    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.