在asp.net中使用Gridview控件进行分页(我使用vb.net进行编码) [英] Paging in Gridview control in asp.net (I am coding using vb.net)
问题描述
大家好,
我从sql数据库中获取数据并在gridview中显示该数据。我有16条记录。当我点击一个按钮时,我想在gridview中以4页显示它们,每页有4条记录。我是vb.net的初学者,也是Asp的初学者。请帮我解决这个问题
我在vb.net中的编码是
Hi all,
I am fetching data from sql database and displaying that data in gridview. I have some 16 records. And when I click on a button, I want to display them in gridview in 4 pages with 4 records in each page. I am beginner in vb.net and also in Asp. Please help me to solve this
My coding in vb.net is
If IsPostBack Then
Dim sql As String
Dim adapter As New SqlDataAdapter
Dim ds As New DataSet
Dim Command As SqlCommand = New SqlCommand
'Dim Command1 As SqlCommand = New SqlCommand
Dim connection As SqlConnection = New SqlConnection("Server=pc5426;uid=so;pwd=intel;database=IDB")
sql = "select * from tblquestionbank2"
Try
connection.Open()
Command = New SqlCommand(sql, connection)
adapter.SelectCommand = Command
adapter.Fill(ds)
'GridView1.DataSource = Command.ExecuteReader
GridView1.DataSource = ds.Tables(0)
GridView1.DataBind()
connection.Close()
Catch ex As Exception
End Try
End if
End Sub
和
And
Private Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
GridView1.PageIndex = e.NewPageIndex
GridView1.DataBind()
End Sub
我的源代码是:
My source code is:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True"
ShowHeader="True" Width="529px" style="margin-top: 110px" PageSize="3"
AllowPaging="True" BorderColor="White" BorderStyle="Ridge" CellSpacing="1" CellPadding="3">
当我尝试这段代码时,我可以在gri的底部看到编号为1 2 3 4的页面DVIEW。但我只能在第一页看到内容,如果我点击2或3或4. gridview本身就会消失。
先谢谢你...
When I tried this code I can see pages numbered 1 2 3 4 at the bottom of gridview. But I can see only content in 1st page and if I click on 2 or 3 or 4. The gridview itself disappears.
Thanks in Advance...
推荐答案
请参阅此示例并按照建议执行:在ASP.Net Gridview控件中实现分页 [ ^ ]
See this example and implement as suggested there : Implement Pagination in ASP.Net Gridview Control[^]
添加此行
viewState(data)= ds.Tables(0);
之前
GridView1.DataSource = ds.Tables(0)
并修改如下
add this line
viewState("data") = ds.Tables(0);
before
GridView1.DataSource = ds.Tables(0)
and modify as below
Private Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
GridView1.DataSource = viewState("data")
GridView1.PageIndex = e.NewPageIndex
GridView1.DataBind()
End Sub
在GridView1_PageIndexChanging中绑定网格
Bind Your Grid at GridView1_PageIndexChanging
这篇关于在asp.net中使用Gridview控件进行分页(我使用vb.net进行编码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!