日期:2013-09-02  浏览次数:20399 次

下面是一个以查询为主的代码页,用的是datagrid,查询中我使用了一个DropDownList,用来判断是根据学号查询,还是根据用户名查询,至于查询的范围,你可以任意加的,至于显示共有多少页及当前页面的代码,我前面已经发过了:)
     这里用到了一个隐藏域,目的是避免当前页的索引值小于0。


代码如下:

     Sub pagechange(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs)
'//////////////bata2中必须写的,否则将不能换页!
        DataGrid1.CurrentPageIndex = e.NewPageIndex
        bindgrid()
    End Sub

    Public Sub bindgrid()
        Select Case DropDownList1.SelectedIndex
            Case 0
'//////////////在下面的sql语句里你可以增加查询字段
                Dim sql As String = "select * from stu_base where stu_id like '%" & keyword.Text & "%'"
                Dim comm As SqlDataAdapter = New SqlDataAdapter(sql, conn)
                Dim ds As DataSet
                ds = New DataSet()
                comm.Fill(ds, "stu_base")
                If ds.tables("stu_base") Is "" Then
                    DataGrid1.Visible = False
                Else
                    DataGrid1.DataSource = ds.Tables("stu_base").DefaultView
                    If ds.tables("stu_base").rows.count = 0 Then  '//////////////判断数据库是否为空
                        DataGrid1.Visible = False
                        Label6.Visible = True
                    Else
                        Label6.Visible = False
                        DataGrid1.Visible = True
                        DataGrid1.DataBind()
                    End If

                End If
                
            Case 1
                Dim sql As String = "select * from stu_base where name like '%" & keyword.Text & &qu