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