再发一个初级问题,请各位帮忙!

fengyeng 2006-08-22 02:53:03
再发一个初级问题,请各位帮忙,问题解决明天就结贴!
下面是我做的一个分页和排序结合的程序,程序进入后显示20条记录!
点分页每次显示20条,点排序也是每次排序前20记录。
但每次我先点完排序再点分页的时候就会显示40条记录,这时我再分页还是20显示条记录!

我实在搞不清到底是为什么?
我连接也关闭了,dataset也clear了datasource也每次使用前设为空了,怎么就会把排序的记录和分页的记录数加到一起了呢(20+20=40条了)?




代码如下:
Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton1.Click


Dim tablename As String = "rs_grda"
Dim fieldlist As String = "sn,bmbm AS 部门编码,grbm AS 个人编码,xm AS 姓名,sex AS 性别,birthday AS 出生日期,politic AS 政治面貌,zw AS 职务,zc AS 职称,tocurrent AS 入本企业时间,tojtcurrent AS 入集团时间,if_ylbx AS 是否交纳养老保险,dh AS 电话,idencode AS 身份证,byxy AS 毕业学校,bysj AS 毕业时间,if_gd AS 是否股东,introduce_bmbm AS 工资发放部门,flag AS 员工状态,wh AS 文化程度,marry AS 婚否,worker AS 工种,hkxz AS 户口性质,sh_flag AS 审核标志,founder_flag AS 是否是创业老员工"
If hi.Value = "" Then
hi.Value = "xm asc"
End If
Dim orderfield As String = hi.Value

Dim keyfield As String = "sn"
Dim pageindex As Integer = 1
Dim pagesize As Integer = 20
Dim strwhere As String = "xm like '%" & TextBox2.Text.Trim & "%'"
Dim ordertype As Boolean = False
Dim sqlstr As String

sqlstr = sqlcount(tablename, fieldlist, orderfield, keyfield, pageindex, pagesize, strwhere, ordertype)
Label2.Text = sqlstr
'If Not IsPostBack Then

Dim queryStringa As String = sqlstr
Dim connectionString As String = "server=192.168.7.10;uid=dynetuser;pwd=nmamtf;database=dynet"
Dim connection As New SqlConnection(connectionString)
Dim adaptera As New SqlDataAdapter(queryStringa, connection)
Dim dsssa As DataSet = New DataSet()
adaptera.Fill(dsssa, "ddd")

If dsssa.Tables.Count > 0 Then
Label1.Text = dsssa.Tables.Count
GridView1.DataSource = Nothing
GridView1.DataBind()
GridView1.DataSource = dsssa
GridView1.DataMember = "ddd"
GridView1.DataBind()
dsssa.Clear()
connection.Close()
Else

Message.Text = "Unable to connect to the database."

End If


End Sub

————————————————————————————————————
Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView1.Sorting
Dim sortstr As String = e.SortExpression
If ViewState("SortDirction") = "" Then
ViewState("SortDirction") = "desc"
Else
If ViewState("SortDirction").ToString = "asc" Then
ViewState("SortDirction") = "desc"
Else
ViewState("SortDirction") = "asc"
End If
End If

Dim orderfield As String = sortstr & " " & ViewState("SortDirction")
hi.Value = orderfield

Dim queryString As String = "select top 20 sn,bmbm AS 部门编码,grbm AS 个人编码,xm AS 姓名,sex AS 性别,birthday AS 出生日期,politic AS 政治面貌,zw AS 职务,zc AS 职称,tocurrent AS 入本企业时间,tojtcurrent AS 入集团时间,if_ylbx AS 是否交纳养老保险,dh AS 电话,idencode AS 身份证,byxy AS 毕业学校,bysj AS 毕业时间,if_gd AS 是否股东,introduce_bmbm AS 工资发放部门,flag AS 员工状态,wh AS 文化程度,marry AS 婚否,worker AS 工种,hkxz AS 户口性质,sh_flag AS 审核标志,founder_flag AS 是否是创业老员工 from rs_grda where xm like '%" & TextBox2.Text.Trim & "%' order by " & orderfield
Dim connectionString As String = "server=192.168.7.10;uid=dynetuser;pwd=nmamtf;database=dynet"
Dim connection As New SqlConnection(connectionString)
Dim adapter As New SqlDataAdapter(queryString, connection)
Dim dsss As DataSet = New DataSet()
adapter.Fill(dsss, "ddd")

If dsss.Tables.Count > 0 Then
Label1.Text = dsss.Tables.Count
GridView1.DataSource = Nothing
GridView1.DataSource = dsss
GridView1.DataMember = "ddd"
GridView1.DataBind()
dsss.Clear()

connection.Close()
Else

Message.Text = "Unable to connect to the database."

End If

'Dim conn As New SqlConnection("server=192.168.7.10;uid=dynetuser;pwd=nmamtf;database=dynet")
'Dim sql As String = ("drop table new table")
End Sub
————————————————————————————
...全文
266 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
smalladam 2006-08-23
  • 打赏
  • 举报
回复
对VB不熟悉呀,绑顶
fengyeng 2006-08-23
  • 打赏
  • 举报
回复
我用的是gridview
我感觉上面的代码和c#也没差很多啊!
这个到底是什么现象呢?
tqg1023 2006-08-23
  • 打赏
  • 举报
回复
不懂VB,不过你可以看看DataGrid的属性设置,包括自动生成列,自定义分页、保存视图状态等。
fengyeng 2006-08-23
  • 打赏
  • 举报
回复
up
fengyeng 2006-08-23
  • 打赏
  • 举报
回复
我好像是发现点问题了,正在解决中。。。。。。
fengyeng 2006-08-22
  • 打赏
  • 举报
回复
我的分页程序和排序程序每次都是重新生成dataset然后将gridview的datasource设为空然后再给它重设值的!

怎么会这样?
yuchangmao 2006-08-22
  • 打赏
  • 举报
回复
你这个样子写肯定不对:ds.Tables.Count,应该是:ds.Tables[0].Rows.Count
leafsword_519 2006-08-22
  • 打赏
  • 举报
回复
1、程序太多,且是VB,看不出问题
2、楼主你还是设置断点一步一步跟一下,看程序的流程是否按照你的意愿进行,这样能很快找出问题。
疯狂秀才2018 2006-08-22
  • 打赏
  • 举报
回复
对VB不熟悉呀,绑顶,出现这种情况应该是在排序里与原有数据进行了累加,
如果点排序的字段先清空grid里的数据,显示的据应该是指序后的数据,但这样会不会就只显示20条呢,我没有试过。。。。
fengyeng 2006-08-22
  • 打赏
  • 举报
回复
up
csover8 2006-08-22
  • 打赏
  • 举报
回复
mark
fengyeng 2006-08-22
  • 打赏
  • 举报
回复
up
冰宇枫 2006-08-22
  • 打赏
  • 举报
回复
不懂VB
fengyeng 2006-08-22
  • 打赏
  • 举报
回复
Function sqlcount(ByVal tablename As String, ByVal fieldlist As String, ByVal orderfield As String, ByVal keyfield As String, ByVal pageindex As Integer, ByVal pagesize As Integer, ByVal strwhere As String, ByVal odertype As Boolean)
Dim sqlstr As String
Dim pagea As Integer, pageb As Integer
pagea = pageindex * pagesize
pageb = (pageindex + 1) * pagesize
sqlstr = "select * from (select top " & pageb & " " & fieldlist & " from " & tablename & " where " & strwhere & " order by " & orderfield & ") as d where " & keyfield & " not in (select top " & pagea & " " & keyfield & " from " & tablename & " where " & strwhere & " order by " & orderfield & ")"

Return sqlstr
End Function
Function GetData(ByVal queryString As String)
Dim connectionString As String = "server=192.168.7.10;uid=dynetuser;pwd=nmamtf;database=dynet"
Dim ds As New DataSet()
Try
Dim connection As New SqlConnection(connectionString)
Dim adapter As New SqlDataAdapter(queryString, connection)
adapter.Fill(ds)
connection.Close()
Catch ex As Exception
Message.Text = "Unable to connect to the database."
End Try
Return ds

End Function

Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then

Dim queryString As String = "select top 20 sn, bmbm AS 部门编码, grbm AS 个人编码, xm AS 姓名, sex AS 性别, birthday AS 出生日期, politic AS 政治面貌, zw AS 职务, zc AS 职称, tocurrent AS 入本企业时间,tojtcurrent AS 入集团时间,if_ylbx AS 是否交纳养老保险, dh AS 电话,idencode AS 身份证, byxy AS 毕业学校, bysj AS 毕业时间, if_gd AS 是否股东, introduce_bmbm AS 工资发放部门,flag AS 员工状态, wh AS 文化程度, marry AS 婚否,worker AS 工种, hkxz AS 户口性质,sh_flag AS 审核标志, founder_flag AS 是否是创业老员工 from rs_grda"

Dim ds As DataSet = GetData(queryString)
If ds.Tables.Count > 0 Then
Label1.Text = ds.Tables.Count
GridView1.DataSource = Nothing
GridView1.DataSource = ds
GridView1.DataBind()
ds.Clear()
Else

Message.Text = "Unable to connect to the database."

End If
End If




End Sub

62,050

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧