我有一个项目,我将数据库连接到vb.net中的Windows应用程序。我需要制作以特定方式对数据进行排序的选项卡。例如,它是一个带有姓名等的学生数据库。所以当我按下"学生按姓氏"选项卡,它按姓氏按升序对数据进行排序。所以,我做了一个带有按钮的查询,但我不知道如何在单击标签页时自动执行按钮。用户无法控制排序。标签本身就是这样做的。任何帮助都会很棒!我一直在寻找帮助。
Private Sub TabPage2_Click(sender As Object, e As EventArgs) Handles tbLastName.Click
TabPage1.Hide() "this is made to get rid of previous page, but may be unnecessary"
tbLastName.Show() "this is supposed to show the data"
Sort_By_LastNameToolStripButton.Enabled() "My attempt to execute code from the query button that I made hidden"
End Sub
答案 0 :(得分:0)
尝试使用tab控件Selected事件来运行数据库代码
答案 1 :(得分:0)
保留查询数据的副本,然后根据选择的选项卡对其进行排序。使用TabControl.SelectedIndexChanged
事件
Private queryData As IEnumerable(Of Student)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
queryData = QueryStudentsFromDatabase()
End Sub
Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles TabControl1.SelectedIndexChanged
Select Case TabControl1.SelectedIndex
Case 0 ' Last Name
Dim displayData = queryData.OrderBy(Function(s) s.LastName)
Case 1 ' Student ID
Dim displayData = queryData.OrderBy(Function(s) s.ID)
End Select
ListView1.DataSource = displayData ' or whatever
End Sub
这是一个非常简单的例子。为了简洁起见,它正在UI线程上执行工作。如果您愿意,可以处理卸载工作。