如何在数据表的行中前后移动? [英] How can I move forward and backward through rows of a datatable?

查看:124
本文介绍了如何在数据表的行中前后移动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有4个按钮(Next,Previous,First,Last)在数据表的行中移动。

按钮First和Last正常工作但其他按钮不正常如果我按下它们,它们会一直指向记录1)。

当我按下最后一个按钮时它会转到最后一个记录,但是当我按下下一个按钮时它会转到第一个记录而不是告诉我没有更多记录。

这是我的表格图片:表格

这是我的代码下面和表格代码:



我尝试过:



  Imports  Microsoft.VisualBasic 
Imports System.Data.SqlClient
Imports System.Data

Partial P3
继承 System.Web.UI.Page
Dim ds 作为 DataSet
Dim inc As Integer
Dim MaxRows As 整数
Dim tbl As DataTable
私有 Sub NavigateRecords()
Gidbx.Text = tbl.Rows(inc).Item( 0
Gnamebx.Text = tbl.Rows(inc).Item( 1
结束 Sub

受保护的 Sub Page_Load(发件人作为 对象,e 作为 EventArgs)句柄 < span class =code-keyword> Me .Load

Response.Write(Session( slctdGarage))
Dim strConnectionString As 字符串
Dim instsqlconnection As SqlConnection
strConnectionString = Integrated Security = SSPI; Initial Catalog = MySetOfGarages; Data Source =。
instsqlconnection = SqlConnection(strConnectionString)
instsqlconnection.Open()
Dim da 作为 SqlDataAdapter( Select * From Garage,instsqlconnection)
da.Fill(ds, Grg
tbl = ds.Tables ( Grg
instsqlconnection.Close()
MaxRows = tbl。 Rows.Count
inc = -1


End Sub

受保护的 Sub Nextrec_Click(sender 作为 对象,e 作为 EventArgs)句柄 Nextrec.Click
如果 inc<> MaxRows - 1 然后
inc = inc + 1
NavigateRecords()
其他
Response.Write( 没有更多行
结束 如果

结束 Sub

受保护的 Sub Previousrec_Click(发件人 As 对象,e 作为 EventArgs)句柄 Previousrec。点击
如果 inc> 0 然后
inc = inc - 1
NavigateRecords()
ElseIf inc = -1 然后
Response.Write( 还没有记录
ElseIf inc = 0 然后
Response.Write(< span class =code-string> First Record

结束 如果

结束 Sub

受保护的 Sub Lastrec_Click(发件人< span class =code-keyword> As 对象,e As EventArgs)句柄 Lastrec.Click
< span class =code-keyword>如果 inc<> MaxRows - 1 然后
inc = MaxRows - 1
NavigateRecords()
结束 如果

结束 Sub

受保护的 Sub Firstrec_Click(发件人作为 对象,e As EventArgs)句柄 Firstrec.Click
如果 inc<> 0 然后
inc = 0
NavigateRecords()
结束 如果

结束 Sub
结束 < span class =code-keyword> Class





 <%@     Page    语言  =  VB    AutoEventWireup   =  false    CodeFile   =  P3.aspx.vb   继承  =  P3   %>  


< 表格 id = form1 runat = server >
< p >
id Garage
< asp:TextBox ID = Gidbx runat = 服务器 > < / asp:TextBox >
< / p >
< p >
GarageName
< asp:TextBox ID = Gnamebx runat = 服务器 > < / asp:TextBox >

< asp:按钮 ID = Firstrec runat = server 文字 = << / >

< asp:按钮 ID < span class =code-keyword> =
Previousrec runat = server 文字 = < / >

< asp:按钮 ID = Ne xtrec runat = server 文本 = > / >

< asp:按钮 ID = Lastrec runat = server 文字 = >> / >
< / p >
< p >

< asp:按钮 ID = Button1 runat = server 文本 = 添加记录 / >

< asp:按钮 ID = Button2 runat = server 文字 = 更新Rec / >

< asp:Button ID = Button3 runat = server 文本 = 删除Rec / >
< / p >
< / form >

解决方案

在这个类似的问题上参考我的常见答案,但它是用C#编写的,试着理解逻辑和在你的应用程序中实现



第一个下一个上一个按钮的代码和页面窗口中的显示数据C#application [ ^ ]





使用它将c#转换为VB CodeTranslator:从c#到VB的代码翻译 [ ^ ]

I have 4 buttons (Next,Previous,First,Last) which moves through the rows of a datatable.
The buttons First and Last are working correctly but the other buttons are not(nothing happens if i press them,they keep pointing to record 1).
When i press the Last button it goes to the last record,but when i press the next button it takes me to the first record instead of telling me "No more Records".
This is a pic of my form: Form
This is my code Below and the form code:

What I have tried:

Imports Microsoft.VisualBasic
Imports System.Data.SqlClient
Imports System.Data

Partial Class P3
    Inherits System.Web.UI.Page
    Dim ds As New DataSet
    Dim inc As Integer
    Dim MaxRows As Integer
    Dim tbl As DataTable
    Private Sub NavigateRecords()
        Gidbx.Text = tbl.Rows(inc).Item(0)
        Gnamebx.Text = tbl.Rows(inc).Item(1)
    End Sub

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
       
        Response.Write(Session("slctdGarage"))
        Dim strConnectionString As String
        Dim instsqlconnection As SqlConnection
        strConnectionString = "Integrated Security=SSPI;Initial Catalog=MySetOfGarages;Data Source=."
        instsqlconnection = New SqlConnection(strConnectionString)
        instsqlconnection.Open()
        Dim da As New SqlDataAdapter("Select * From Garage", instsqlconnection)
        da.Fill(ds, "Grg")
        tbl = ds.Tables("Grg")
        instsqlconnection.Close()
        MaxRows = tbl.Rows.Count
        inc = -1

      
    End Sub

    Protected Sub Nextrec_Click(sender As Object, e As EventArgs) Handles Nextrec.Click
        If inc <> MaxRows - 1 Then
            inc = inc + 1
            NavigateRecords()
        Else
            Response.Write("No More Rows")
        End If

    End Sub

    Protected Sub Previousrec_Click(sender As Object, e As EventArgs) Handles Previousrec.Click
        If inc > 0 Then
            inc = inc - 1
            NavigateRecords()
        ElseIf inc = -1 Then
            Response.Write("No Records Yet")
        ElseIf inc = 0 Then
            Response.Write("First Record")

        End If

    End Sub

    Protected Sub Lastrec_Click(sender As Object, e As EventArgs) Handles Lastrec.Click
        If inc <> MaxRows - 1 Then
            inc = MaxRows - 1
            NavigateRecords()
        End If

    End Sub

    Protected Sub Firstrec_Click(sender As Object, e As EventArgs) Handles Firstrec.Click
        If inc <> 0 Then
            inc = 0
            NavigateRecords()
        End If

    End Sub
End Class



<%@ Page Language="VB" AutoEventWireup="false" CodeFile="P3.aspx.vb" Inherits="P3" %>


<form id="form1" runat="server">
    <p>
         id Garage      
        <asp:TextBox ID="Gidbx" runat="server"></asp:TextBox>
         </p>
    <p>
         GarageName 
        <asp:TextBox ID="Gnamebx" runat="server"></asp:TextBox>
            
        <asp:Button ID="Firstrec" runat="server" Text="<<" />
 
        <asp:Button ID="Previousrec" runat="server" Text="<" />
 
        <asp:Button ID="Nextrec" runat="server" Text=">" />
 
        <asp:Button ID="Lastrec" runat="server" Text=">>" />
    </p>
    <p>
                         
        <asp:Button ID="Button1" runat="server" Text="Add Record" />
               
        <asp:Button ID="Button2" runat="server" Text="Update Rec" />
        
        <asp:Button ID="Button3" runat="server" Text="Delete Rec" />
    </p>
</form>

解决方案

refer my prev answer on this similar question, but it was written on C#, try to understand the logic and implement in your app

Code for first next last previous button and display data in page windows C# application[^]


use this for converting the c# to VB CodeTranslator: Code Translation From c# to VB [^]


这篇关于如何在数据表的行中前后移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆