Results 1 to 8 of 8

Thread: DataGridView + movenextbutton

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    92

    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??

  2. #2
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    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:
    1. Private Sub BindingSource1_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles BindingSource1.PositionChanged
    2.     Me.nextBtn.Enabled = Me.BindingSource1.Position < Me.BindingSource1.List.Count - 2
    3.     Me.previouseBtn.Enabled = Me.BindingSource1.Position > 0
    4. End Sub

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    92

    Re: DataGridView + movenextbutton

    thanks for answering. How can I bind the bindingsource with the dataset and the datagridview? Is it through coding?

  4. #4
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    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.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    92

    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

  6. #6
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: DataGridView + movenextbutton

    it should be:
    vb Code:
    1. 'Set the DataGridView properties to bind it to our data...
    2. Me.BindingSource1.DataSource = objDataSet
    3. Me.DataGridView1.AutoGenerateColumns = True
    4. Me.DataGridView1.DataSource = Me.BindingSource1
    5. Me.DataGridView1.DataMember = "Table_1"

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    92

    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?

  8. #8
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    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
  •  



Click Here to Expand Forum to Full Width