首页 > 开发 > 综合 > 正文

个性化查询(具有分类模糊查询、换页等功能)

2024-07-21 02:17:01
字体:
来源:转载
供稿:网友

下面是一个以查询为主的代码页,用的是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 & "%'"
                dim comm as sqldataadapter = new sqldataadapter(sql, conn)
                dim ds as dataset
                ds = new dataset()
                comm.fill(ds, "stu_base")
                datagrid1.datasource = ds.tables("stu_base").defaultview
                datagrid1.databind()
        end select
    end sub

    private sub querry1_click(byval sender as system.object, byval e as system.eventargs) handles querry1.click
'//////////////隐藏域,用来转换数据
        keyword.text = querry.text
        datagrid1.currentpageindex = 0
        bindgrid()
    end sub
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表