|
-
Feb 4th, 2009, 12:02 PM
#1
Thread Starter
Lively Member
DataGridView + movenextbutton
Hi, I have a datagridview that is filled from a database through coding. I also have two buttons (movenext and moveprevious buttons) that I want to make active the next or the previous row of the Datagridview. I know that this can also be done with bindingnavigator control but I need code to do this.
Does anyone know how to do this? Or does anyone know if it is possible to see the code that is used in the bindingnavigator control??
-
Feb 4th, 2009, 12:17 PM
#2
Re: DataGridView + movenextbutton
You should have BindingSource object binded to the dataSet and the DataGridView binded to the BindingSource. The BindingSource fires PositionChanged event. There you can set the button Next and the Previous enabled properties. For example:
vb Code:
Private Sub BindingSource1_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles BindingSource1.PositionChanged
Me.nextBtn.Enabled = Me.BindingSource1.Position < Me.BindingSource1.List.Count - 2
Me.previouseBtn.Enabled = Me.BindingSource1.Position > 0
End Sub
-
Feb 4th, 2009, 12:35 PM
#3
Thread Starter
Lively Member
Re: DataGridView + movenextbutton
thanks for answering. How can I bind the bindingsource with the dataset and the datagridview? Is it through coding?
-
Feb 4th, 2009, 12:41 PM
#4
Re: DataGridView + movenextbutton
You can do it at design time. Just drag and drop BindingSource component from the VS toolbox. Set its datasource to the dataset and set the datagrideview datasource to the bindingSource object.
-
Feb 4th, 2009, 12:51 PM
#5
Thread Starter
Lively Member
Re: DataGridView + movenextbutton
I did what you told me but the two buttons are now unabled to be clicked.
I am giving you some details: The dataDRidView datasource is BindingSource1 .
The datasource of the bindingsource is CommercialDataSet
I think this are as you told me.
And the code I wrote is:
Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlClient
Public Class Form3
Dim objConnection As New SqlConnection _
("server=EROM-PC\COMMERCIAL;database=Commercial;user id=sa;password=dfs234;")
Dim objDataAdapter As New SqlDataAdapter()
Dim objDataSet As New DataSet()
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Set the SelectCommand properties...
objDataAdapter.SelectCommand = New SqlCommand()
objDataAdapter.SelectCommand.Connection = objConnection
objDataAdapter.SelectCommand.CommandText = _
"SELECT Code, Description, BarCode, MM, Remainder FROM Table_1 "
objConnection.Open()
' Fill the DataSet object with data...
objDataAdapter.Fill(objDataSet, "Table_1")
objConnection.Close()
' Set the DataGridView properties to bind it to our data...
DataGridView1.AutoGenerateColumns = True
DataGridView1.DataSource = objDataSet
DataGridView1.DataMember = "Table_1"
End Sub
Private Sub BindingSource1_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles BindingSource1.PositionChanged
Me.nextBtn.Enabled = Me.BindingSource1.Position < Me.BindingSource1.List.Count - 2
Me.previouseBtn.Enabled = Me.BindingSource1.Position > 0
End Sub
End Class
-
Feb 4th, 2009, 12:59 PM
#6
Re: DataGridView + movenextbutton
it should be:
vb Code:
'Set the DataGridView properties to bind it to our data...
Me.BindingSource1.DataSource = objDataSet
Me.DataGridView1.AutoGenerateColumns = True
Me.DataGridView1.DataSource = Me.BindingSource1
Me.DataGridView1.DataMember = "Table_1"
-
Feb 4th, 2009, 01:05 PM
#7
Thread Starter
Lively Member
Re: DataGridView + movenextbutton
I did this but the problems is that when I run the program I can't click the buttons because they are disabled. Why this happened?
-
Feb 4th, 2009, 01:10 PM
#8
Re: DataGridView + movenextbutton
can you tell me what is the count for Me.BindingSource1.List.Count when it finishes reading from db?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|