Here is a picture of a form. http://postimage.org/image/a813nony5/

Please help:
I have a form with:
- 4 textboxes (textbox 11,21,311 & 41)
- 4 buttons (add, delete, save, refresh)
- 1 DataGridView (MS Access database connected)
- 1 filter to filter the data through DatagridView

Here is the story:
When I use buttons to navigate the database, I have to click ten times on button "Next" to go to the tenth record.
Code for that is:

(...)
BindingContext(DatasetTelefonija1, "Telefoni").Position = BindingContext(DatasetTelefonija1, "Telefoni").Position + 1
(...)

What if I have onehundred records?

So, I explored the internet and made another code: using DataGridView to click items and sending the data to text boxes.
Code for that is:
(...)
TextBox21.Text = DataGridView1.Item(1, DataGridView1.CurrentRow.Index).Value.ToString
(...)
Data is shown in textboxes, but problem is when I click on button Previous or Next, it gives me an error.
Problem is when you select data in datagrid, it is shown in textboxes but it is not shown in (i think) in "DatasetTelefonija", so when I use Delete button, it deletes the first record.

Also, I need to add that textboxes have properties -> data -> data bindings set.

Another Question.
When I use filter and start typing, results are not shown until I write an exact word.
Example: word in database is Animal. When I start typing letters "An" it doesnt show me a single word until I type a whole word "Animal".

Code for that is:
(..)
Me.TelefoniBindingSource.Filter = "[NameandSurname] = '" & TextBox5.Text & "'"
(...)
Also, problem with this "filter search" is when I delete everything, datagridview doesnt show a single record.
I used DataGridView.Refresh() method, but nothing.

Here is the whole code:

Public Class Telefon

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    BindingContext(DatasetTelefonija1, "Telefoni").Position = BindingContext(DatasetTelefonija1, "Telefoni").Position + 1
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    BindingContext(DatasetTelefonija1, "Telefoni").Position = BindingContext(DatasetTelefonija1, "Telefoni").Position - 1
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    ImenikForma.Show()
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    BindingContext(DatasetTelefonija1, "Telefoni").RemoveAt(BindingContext(DatasetTelefonija1, "Telefoni").Position)
End Sub

Private Sub DataGrid1_Navigate(ByVal sender As System.Object, ByVal ne As System.Windows.Forms.NavigateEventArgs)
    OleDbDataAdapter1.Fill(DatasetTelefonija1)

End Sub

Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged
    Me.TelefoniBindingSource.Filter = "[NameandSurname] = '" & TextBox5.Text & "'"
    DataGridView1.Refresh()

End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
    Me.Validate()
    Me.TelefoniBindingSource.EndEdit()
    Me.OleDbDataAdapter1.Update(Me.DatasetTelefonija1)
    MsgBox("Podaci spremljeni")
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
    OleDbDataAdapter1.Fill(DatasetTelefonija1)
End Sub

Private Sub Telefon_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    OleDbDataAdapter1.Fill(DatasetTelefonija1)
    DataGridView1.Columns(0).HeaderText = "Redni broj"
    DataGridView1.Columns(1).HeaderText = "Ime i prezime"
    DataGridView1.Columns(2).HeaderText = "Telefon"
    DataGridView1.Columns(3).HeaderText = "Ostalo"

End Sub

Private Sub Telefon_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseHover
    DataGridView1.Refresh()

End Sub
Private Sub TextBox6_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox6.TextChanged
    Me.TelefoniBindingSource.Filter = "[Telefon] = '" & TextBox6.Text & "'"
    DataGridView1.Refresh()
End Sub

Private Sub TextBox7_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox7.TextChanged
    Me.TelefoniBindingSource.Filter = "[Ostalo] = '" & TextBox7.Text & "'"
    DataGridView1.Refresh()
End Sub

Private Sub Alo()
    DataGridView1.Columns(3).HeaderText = "Percentage"
    DataGridView1.Refresh()
End Sub
Private Sub SetBorderAndGrid()
    With Me.DataGridView1
        .GridColor = Color.DarkBlue
        .BorderStyle = BorderStyle.FixedSingle
    End With
End Sub

Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
    SetBorderAndGrid()
    Alo()
End Sub

Sub GridValues()
    TextBox11.Text = DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value.ToString 'in the place of column name u can also place the column index .
    TextBox21.Text = DataGridView1.Item(1, DataGridView1.CurrentRow.Index).Value.ToString
    TextBox311.Text = DataGridView1.Item(2, DataGridView1.CurrentRow.Index).Value.ToString
    TextBox41.Text = DataGridView1.Item(3, DataGridView1.CurrentRow.Index).Value.ToString
End Sub


Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
    GridValues()
End Sub


Private Sub DataGridView1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
    GridValues()
End Sub

Private Sub DataGridView1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyUp
    GridValues()
End Sub

End Class

Recommended Answers

All 3 Replies

Found solution for filter data.
Still don't know the navigation problem.

I recommend that you create a BindingNavigator to manage navigation.
Your current implimentation has no error checking and the navigator will do all that for you.
http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingnavigator(v=vs.90).aspx

With a navigator you can define the buttons that handle navigation. The one draw back is that they need to be ToolStrip items. Add an undocked toolstrip for each button that you want to have in your form and set the autosize property to false; also set the LayoutStyle to Table. To replace your current buttons, add a Toolstipbutton to the toolstrip and make it look like your original button. You can also add buttons for goto first and goto end. To allow jumping to a specific row (PositionItem), add a toolstriptextbox. To get a total of the row, add another toolstriptextbox for CountItem and make it read-only.

If you do this and modify your code as follows, you can delete all your existing button-click code and your "gridvalues" method code.

On an issue of readability, I suggest that you name the buttons and textboxes to reflect what they actually display on the form.

Private MyNavigator as New BindingNavigator(False)

Private Sub Telefon_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    OleDbDataAdapter1.Fill(DatasetTelefonija1)
    DataGridView1.Columns(0).HeaderText = "Redni broj"
    DataGridView1.Columns(1).HeaderText = "Ime i prezime"
    DataGridView1.Columns(2).HeaderText = "Telefon"
    DataGridView1.Columns(3).HeaderText = "Ostalo"


    With MyNavigator
       .Parent = Me
       .BindingSource = Me.TelefoniBindingSource
       .PositionItem = ToolStripTextBox1
       .CountItem = ToolStripTextBox2
       .MovePreviousItem = ToolStripButton1
       .MoveNextItem = ToolStripButton2
       .AddNewItem = ToolStripButton3
       .DeleteItem = ToolStripButton4
       .MoveFirstItem = ToolStripButton5
       .MoveLastItem = ToolStripButton6
    End With

    'You may want to make these textboxes read-only.
    TextBox1.DataBindings.Add("Text", _
                               Me.TelefoniBindingSource, _
                               "Redni broj", _
                               False, _
                               DataSourceUpdateMode.OnPropertyChanged)

    TextBox21.DataBindings.Add("Text", _
                               Me.TelefoniBindingSource, _
                               "Ime i prezime", _
                               False, _
                               DataSourceUpdateMode.OnPropertyChanged)

    TextBox311.DataBindings.Add("Text", _
                               Me.TelefoniBindingSource, _
                               "Telefon", _
                               False, _
                               DataSourceUpdateMode.OnPropertyChanged)

    TextBox411.DataBindings.Add("Text", _
                               Me.TelefoniBindingSource, _
                               "Ostalo", _
                               False, _
                               DataSourceUpdateMode.OnPropertyChanged)

End Sub

Thank you for response.
I'm sorry but I prefer a program without Binding Navigator. Don't ask why...

My new code is this and somehow is working... (I edited the names for you to understand it better)

Question: 1.How can make copy-paste method in datagrid and database?
Example: I click on DataGridView on selecteded Row, then I press copy -> then press paste -> new window/form/dialog box pop's out with data in it, if edit data is needed, then press button to save changes.

  1. I don't know if method I'm using to refresh datagrid view is Ok?
    Procedures related to this questions are: PhoneNumber_MouseHover and button6 and Refresh2()

  2. In this form I have a button to add new contact in phonebook. When pressed, new window pop's out to add new contact. I made this form, but I needed to put in again, BindingSource, Dataset and Adapter. Is this Ok?

I mean, it works, but I don't know if it will be "successful" on second computer.

Once again, thank you for response.

Public Class Phone_number
    Dim con As New OleDb.OleDbConnection
    Dim cmdOle As New OleDb.OleDbCommand
    Dim dsOle As New DataSet
    Dim da As New OleDb.OleDbDataAdapter
    Dim dtOle As New DataTable
    Dim sql As String

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 
        BindingContext(DatasetTelephones1, "Telephones").Position = BindingContext(DatasetTelephones1, "Telephones").Position + 1
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        BindingContext(DatasetTelephones1, "Telephones").Position = BindingContext(DatasetTelephones1, "Telephones").Position - 1
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        PhonebookAddwithNewForm.Show()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        BindingContext(DatasetTelephones1, "Telephones").RemoveAt(BindingContext(DatasetTelephones1, "Telephones").Position)
        Me.OleDbDataAdapter1.Update(Me.DatasetTelephones1)
    End Sub

    Private Sub DataGrid1_Navigate(ByVal sender As System.Object, ByVal ne As System.Windows.Forms.NavigateEventArgs)
        OleDbDataAdapter1.Fill(DatasetTelephones1)

    End Sub

    Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged
        con.ConnectionString = ("Provider = Microsoft.JET.OLEDB.4.0;Data Source= db6.mdb")
        Try
            dsOle.Clear()
            dtOle.Clear()
            cmdOle = con.CreateCommand
            cmdOle.CommandText = "SELECT * FROM Telephones where Nameandsurname & Phone_number & Misc LIKE '%" & Trim(TextBox5.Text) & "%'"
            da.SelectCommand = cmdOle
            da.Fill(dsOle, "Telephones") 
            DataGridView1.DataSource = dsOle
            DataGridView1.DataMember = "Telephones" 
            DataGridView1.ReadOnly = True
        Catch ex As Exception
            MsgBox("Greška: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OkOnly, "Koneksi Error !!")
        End Try
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        'Me.Validate()
        Me.TelephonesBindingSource.EndEdit()
        Me.OleDbDataAdapter1.Update(Me.DatasetTelephones1)
        MsgBox("Promjene spremljene")
    End Sub
    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Refresh2()

    End Sub
    Private Sub Refresh2()
        Try
        Catch ex As Exception
            MsgBox(" " & ex.Message)
        End Try
        OleDbDataAdapter1.Fill(DatasetTelephones1)
        DataGridView1.DataSource = DatasetTelephones1
    End Sub
    Private Sub Phone_number_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        OleDbDataAdapter1.Fill(DatasetTelephones1)
        DataGridView1.Columns(0).HeaderText = "Number #"
        DataGridView1.Columns(1).HeaderText = "Name and surname"
        DataGridView1.Columns(2).HeaderText = "Phone_number"
        DataGridView1.Columns(3).HeaderText = "Misc"
        Refresh()
    End Sub
    'Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'proba1.TextBox1.Text = DataGridView1.Item(1, 'DataGridView1.CurrentRow.Index).Value.ToString
        'proba1.Show()
    'End Sub
    Sub GridValues()
        TextBox11.Text = DataGridView1.Item(0, dataGridView1.CurrentRow.Index).Value.ToString 
        TextBox21.Text = DataGridView1.Item(1, DataGridView1.CurrentRow.Index).Value.ToString
        TextBox311.Text = DataGridView1.Item(2, DataGridView1.CurrentRow.Index).Value.ToString
        TextBox41.Text = DataGridView1.Item(3, DataGridView1.CurrentRow.Index).Value.ToString
    End Sub


    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        GridValues()
    End Sub


    Private Sub DataGridView1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
        GridValues()
    End Sub

    Private Sub DataGridView1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyUp
        GridValues()
    End Sub
    Private Sub LoadData()

        con.ConnectionString = ("Provider = Microsoft.JET.OLEDB.4.0;Data Source= db6.mdb")
        Try
            cmdOle = con.CreateCommand
            cmdOle.CommandText = "SELECT * FROM Telephones" 
            da.SelectCommand = cmdOle
            da.Fill(dsOle, "Telephones") 
            DataGridView1.DataSource = dsOle
            DataGridView1.DataMember = "Telephones" 
            DataGridView1.ReadOnly = True
        Catch ex As Exception
            MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OkOnly, "Koneksi Error !!")
        End Try
    End Sub
    Private Sub Search_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadData()
    End Sub
    Private Sub Phone_number_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseHover
        Refresh2()
    End Sub
    End Class
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.