绑定导航器按钮的编码

时间:2014-12-11 21:33:35

标签: vb.net bindingnavigator

这是我第一次使用绑定导航器,我无法弄清楚如何让它正常工作。在我所做的所有示例中,没有代码显示大多数按钮,它们只是工作。我的几乎都是灰色的。任何帮助指出我正确的方向是值得赞赏的。感谢。

Option Explicit On
Option Strict On
Option Infer Off

Public Class frmMain

    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'TripsDataSet.tblTrips' table. You can move, or remove it, as needed.
        Me.TblTripsTableAdapter.Fill(Me.TripsDataSet.tblTrips)
    End Sub

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub btnAll_Click(sender As Object, e As EventArgs) Handles btnAll.Click
        'displays all trips
        Me.TblTripsTableAdapter.Fill(Me.TripsDataSet.tblTrips)
        TblTripsDataGridView.DataSource = TripsDataSet.tblTrips
    End Sub


    Private Sub btnCount_Click(sender As Object, e As EventArgs) Handles btnCount.Click
        'displays either business trips or pleasure trips
        Dim foundRows() As TripsDataSet.tblTripsRow
        Dim businesspleasure As String = ""
        If radBusiness.Checked Then
            businesspleasure = "B"
        ElseIf radPleasure.Checked Then
            businesspleasure = "P"
        End If

        foundRows = CType(Me.TripsDataSet.tblTrips.Select("BusinessPleasure = '" & businesspleasure & "'"), Trips_Project.TripsDataSet.tblTripsRow())

        Dim ds As New TripsDataSet.tblTripsDataTable
        For Each row As DataRow In foundRows
            ds.ImportRow(row)
        Next
        TblTripsDataGridView.DataSource = ds

        MessageBox.Show("Trips:  " & TblTripsDataGridView.RowCount - 1)
    End Sub

    Private Sub btnTrips_Click(sender As Object, e As EventArgs) Handles btnTrips.Click
        Dim foundTrips() As TripsDataSet.tblTripsRow
        Dim originTrip As String
        Dim destinTrip As String

        If radOrAtl.Checked Then
            originTrip = "Atlanta"
        ElseIf radOrChi.Checked Then
            originTrip = "Chicago"
        ElseIf radOrLa.Checked Then
            originTrip = "Los Angeles"
        Else
            originTrip = "Nashville"
        End If

        If radDestAtl.Checked Then
            destinTrip = "Atlanta"
        ElseIf radDestChi.Checked Then
            destinTrip = "Chicago"
        ElseIf radDestLa.Checked Then
            destinTrip = "Los Angeles"
        Else
            destinTrip = "Nashville"
        End If
        foundTrips = CType(Me.TripsDataSet.tblTrips.Select(String.Format("Origin = '{0}' AND Destination = '{1}'", originTrip, destinTrip)), Trips_Project.TripsDataSet.tblTripsRow())

        Dim ds As New TripsDataSet.tblTripsDataTable
        For Each row As DataRow In foundTrips
            ds.ImportRow(row)
        Next

        TblTripsDataGridView.DataSource = ds

        MessageBox.Show("Total Trips:  " & TblTripsDataGridView.RowCount - 1)
    End Sub

    Private Sub BindingNavigatorSaveData_Click(sender As Object, e As EventArgs) Handles BindingNavigatorSaveData.Click
        Try
            Me.Validate()
            Me.TblTripsBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.TripsDataSet)
            MessageBox.Show("Updates saved", "My Trips", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "My Trips", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub

    Private Sub BindingNavigatorAddNewItem_Click(sender As Object, e As EventArgs) Handles BindingNavigatorAddNewItem.Click
        Try
            TblTripsBindingSource.AddNew()
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

    Private Sub BindingNavigatorDeleteItem_Click(sender As Object, e As EventArgs) Handles BindingNavigatorDeleteItem.Click
        Me.BindingNavigator1.BindingSource.RemoveCurrent()
        Try
            Me.Validate()
            Me.TblTripsBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.TripsDataSet)
            MessageBox.Show("Update Successful")
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub
End Class

1 个答案:

答案 0 :(得分:4)

BindingNavigator基本上是BindingSource的UI。 BindingNavigator上的默认项分别映射到关联的BindingSource的MoveFirst方法,MovePrevious方法,Position属性,Count属性,MoveNext方法,MoveLast方法,AddNew方法和RemoveCurrent方法。如果您尚未将BindingSource对象分配给BindingNavigator的BindingSource属性,则这些项目将显示为灰色。

请注意,在btnAll Button的Click事件处理程序中,您将DataTable直接绑定到DataGridView。这使你的BindingNavigator无用。您需要将DataTable绑定到BindingSource,将BindingSource绑定到DataGridView。然后BindingNavigator的项操作BindingSource,从而影响BindingSource绑定的任何内容,即DataGridView。如果BindingSource没有绑定到DataGridView,那么BindingNavigator不会对DataGridView产生任何影响。