好的,首先。显然,我们不会在代码中一遍又一遍地键入连接信息。当您需要更改哪个数据库时会发生什么。
所以,去项目->“我的项目名称”属性。
现在,进入设置。并使用连接字符串生成器创建连接设置。您可以在此页面中放入其他方便的设置 - 可能是公司名称(字符串),甚至是税率编号或其他任何内容。
所以你看到了:
当您点击上面的连接构建器时,您当然会看到这些对话框:
选择更改 - 然后选择访问
现在我们有了这个 - 浏览访问文件:
好的,我们执行上述操作,因为现在我们有一个地方用于连接。
我们还保存了一个键盘,我们不会在代码中输入连接字符串。如前所述,您可以在上面添加各种方便的应用程序设置。
好的,所以我们创建了连接。
现在,下一部分。我们希望允许对我们的数据进行排序。说出名字、酒店名称或城市。
所以,我们进入我们的页面,一个单选按钮列表,然后说一个网格视图。
我们有这个单选按钮:
编辑项目,让我们为用户输入排序选项。
注意第一个选项名字 - 我们还设置了 selected = true (我们的默认排序)
现在打开单选按钮列表的属性表 - 设置为水平
还在上面的单选按钮列表中设置 autopostback = true。
好的,现在拖放到单选按钮列表下方的网格视图中
到目前为止,我们有这个标记:
<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal" CellSpacing="15" AutoPostBack="True">
<asp:ListItem Selected="True" Value="FirstName">By First Name</asp:ListItem>
<asp:ListItem Value="HotelName">by Hotel Name</asp:ListItem>
<asp:ListItem Value="City">by City</asp:ListItem>
</asp:RadioButtonList>
<asp:GridView ID="GridView1" runat="server" CssClass="table" Width="30%">
</asp:GridView>
我还设置了单选按钮列表的单元格间距。
我真的不必输入任何标记,但到目前为止你应该已经有了以上内容。
现在,右键单击 - 查看代码,然后将其连接起来:
我们需要的代码顶部:
Imports System.Data
Imports System.Data.OleDb
我们的代码现在看起来像:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
LoadGrid()
End If
End Sub
Sub LoadGrid()
Using conn As New OleDbConnection(My.Settings.AccessDB)
Dim strSQL As String =
"SELECT FirstName, LastName, City, HotelName, Description FROM tblHotels
ORDER BY " &
RadioButtonList1.SelectedItem.Value
Using cmdSQL As New OleDbCommand(strSQL, conn)
conn.Open()
Dim rstData As New DataTable
rstData.Load(cmdSQL.ExecuteReader)
GridView1.DataSource = rstData
GridView1.DataBind()
End Using
End Using
End Sub
现在我们看到/拥有了这个:
只有剩下的部分是添加当我们单击单选按钮列表以更改排序时的事件。
因此,RB 列表、事件的属性表,然后选择 on-change 事件
我们现在有了这个代码:
Protected Sub RadioButtonList1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles RadioButtonList1.SelectedIndexChanged
LoadGrid()
End Sub
因此,作为一般规则,我们可以简单地设置/更改/拥有任何 sql,并且我们可以将 SQL 包含在排序中
所以,
SELECT * from tblHotels ORDER BY HotelName.
然后按照上面的方法将它输入到网格中。
编辑:这可能适用于桌面 - 下面的桌面(winforms)
不清楚这是否适用于桌面,但是否适用于桌面?
然后真的和上面的差不多。
我们将一个 GroupBox 放入一个表单中(因此只有一个单选按钮会被选中)。
然后我们放入一个 DataGridView(桌面中没有 GridView 之类的东西)。
我们现在有了这个:
我们可以为第一个单选按钮连接一个点击事件(使用属性表)
然后为接下来的两个单选按钮指定相同的点击事件。
所以,我们现在有了这个代码:
Private Sub RadioGrid_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LoadGrid()
End Sub
Sub LoadGrid()
Using conn As New OleDbConnection(My.Settings.AccessDB)
Dim strSQL As String =
"SELECT FirstName, LastName, City, HotelName, Description FROM tblHotels "
Dim strSortBy As String = ""
if RadioButton1.Checked then strSortBy = "FirstName"
if RadioButton2.Checked then strSortBy = "HotelName"
If RadioButton3.Checked Then strSortBy = "City"
If strSortBy <> "" Then
strSQL &= " ORDER BY " & strSortBy
End If
Using cmdSQL As New OleDbCommand(strSQL, conn)
conn.Open()
Dim rstData As New DataTable
rstData.Load(cmdSQL.ExecuteReader)
DataGridView1.DataSource = rstData
End Using
End Using
End Sub
Private Sub RadioButton1_Click(sender As Object, e As EventArgs) Handles RadioButton1.Click, RadioButton3.Click, RadioButton2.Click
LoadGrid()
End Sub
结果是这样的: