我正在开发一个可视化的基本应用程序,它连接到名为Trips的微软访问数据库。它记录了一个人的商务和休闲旅行。用户应该能够显示商务和休闲旅行的总数,所有旅行以及从特定来源到特定目的地的旅行次数。最后一部分是我遇到麻烦的地方。当我在单选按钮选择后单击btn_Trips时,它会使用所选原点或所选目标拉出所有行,而不是拉动仅包含该特定原点和目标的行,然后显示计数。我不知道如何解决这个问题,所以非常感谢任何帮助。感谢
选项明确开启 选项严格打开 选项推断关闭
公共类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 foundOrigin() As TripsDataSet.tblTripsRow
Dim foundDestin() 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
foundOrigin = CType(Me.TripsDataSet.tblTrips.Select("Origin = '" & originTrip & "'"), Trips_Project.TripsDataSet.tblTripsRow())
If radDestAtl.Checked Then
destinTrip = "Atlanta"
ElseIf radDestChi.Checked Then
destinTrip = "Chicago"
ElseIf radDestLa.Checked Then
destinTrip = "Los Angeles"
Else
destinTrip = "Nashville"
End If
foundDestin = CType(Me.TripsDataSet.tblTrips.Select("Destination = '" & destinTrip & "'"), Trips_Project.TripsDataSet.tblTripsRow())
Dim ds As New TripsDataSet.tblTripsDataTable
For Each row As DataRow In foundOrigin
ds.ImportRow(row)
Next
For Each row As DataRow In foundDestin
ds.ImportRow(row)
Next
TblTripsDataGridView.DataSource = ds
MessageBox.Show("Total Trips: " & TblTripsDataGridView.RowCount - 1)
End Sub
结束班
答案 0 :(得分:0)
看看Linq以及您可以使用的不同方法。我不知道你想要最终得到什么,但如果我正确解释你的解释你只想要这些行的计数。您应该能够将linq(请参阅this page)与数据表一起使用,所以:
Dim query =
from row in Me.TripsDataSet.tblTripsDataTable
where row.Origin = originTrip and row.Destination = destinTrip
select row
MessageBox.Show(String.Format("Total Trips: {0}", query.Count())
答案 1 :(得分:0)
你的问题在这里:
....
foundOrigin = CType(Me.TripsDataSet.tblTrips.Select("Origin = '" & originTrip & "'"), Trips_Project.TripsDataSet.tblTripsRow())
....
foundDestin = CType(Me.TripsDataSet.tblTrips.Select("Destination = '" & destinTrip & "'"), Trips_Project.TripsDataSet.tblTripsRow())
您在一个阵列上选择具有所选原点的行程,并在另一个阵列上与所选目的地一起跳闸。您可以使用AND
在一个句子中选择两者:
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())
我还建议在连接多个字符串时使用String.Format
。
另一个选项是LINQ,如另一个答案中所述,但IMO是一种矫枉过正,因为这是一个简单的案例,因为数据表已经为您提供了一种灵活的方法来过滤行。