我如何分页获取datagridview VB.NET [英] How do I paging for datagridview VB.NET
问题描述
我想在DataGridView中进行分页我看到了一些教程,但是所有用过的数据表和数据集都填充了"Datagridview",但是在我的代码中,我不想显示所有项目,而且我有图片按钮和其他内容.
我想要的是向我的网格中添加分页,以便用户可以选择显示什么并平稳地加载Datagrid视图.我想要的是这样的:
https://www.sourcecodester.com/sites/default/files/images/admin/datagridview_paging.jpg
谢谢.
这是我用来从数据库填充Gridview的代码:
我尝试过的事情:
I want to make Paging in DataGridView I seen some tutorials but all of used data table and data set to fill the ''Datagridview'' but in my code, I don''t want to show all items and also I have an image button and another stuff.
what I want is to add paging to my grid so the user can choose what to show and load the Datagrid view smoothly. what I want is something like this:
https://www.sourcecodester.com/sites/default/files/images/admin/datagridview_paging.jpg
thank you.
This is the code that I Use to fill Gridview from the database:
What I have tried:
Sub FillGrid()
DataGrid1.CancelEdit()
DataGrid1.Columns.Clear()
DataGrid1.DataSource = Nothing
'Delete Button
Dim btnDelete As New DataGridViewButtonColumn()
btnDelete.FlatStyle = FlatStyle.Flat
'Image Button
Dim btnImage As New DataGridViewButtonColumn()
btnImage.FlatStyle = FlatStyle.Flat
Dim column = New CalendarColumn() With {.HeaderText = "Date"}
column.DefaultCellStyle.Format = "dd/MM/yyyy"
column.DataPropertyName = "Date"
DataGrid1.Columns.Insert(0, btnDelete)
DataGrid1.Columns.Add("", "Id")
DataGrid1.Columns.Add("", "Name")
DataGrid1.Columns.Add("", "nickname")
DataGrid1.Columns.Add("", "city")
DataGrid1.Columns.Add("", "phone 1")
DataGrid1.Columns.Add("", "phone 2")
DataGrid1.Columns.Add("", "phone 3")
DataGrid1.Columns.Add("", "Email")
DataGrid1.Columns.Add(btnImage)
DataGrid1.Columns.Add(column)
DataGrid1.Columns.Add("", "image")
DataGrid1.Columns.Add("", "")
DataGrid1.Columns(11).Visible = False
Const sqlstring As String = "Select * FROM customers"
If SQL.conn.State = ConnectionState.Open Then
SQL.conn.Close()
End If
SQL.conn.Open()
Dim adt As New SqlDataAdapter(sqlstring, SQL.conn)
Dim dt As New DataTable
adt.Fill(dt)
Dim i As Integer
For i = 0 To dt.Rows.Count - 1
DataGrid1.Rows.Add()
DataGrid1.Rows(i).Cells(1).Value = dt.Rows(i).ItemArray(0)
DataGrid1.Rows(i).Cells(2).Value = dt.Rows(i).ItemArray(1)
DataGrid1.Rows(i).Cells(3).Value = dt.Rows(i).ItemArray(2)
DataGrid1.Rows(i).Cells(4).Value = dt.Rows(i).ItemArray(3)
DataGrid1.Rows(i).Cells(5).Value = dt.Rows(i).ItemArray(4)
DataGrid1.Rows(i).Cells(6).Value = dt.Rows(i).ItemArray(5)
DataGrid1.Rows(i).Cells(7).Value = dt.Rows(i).ItemArray(6)
DataGrid1.Rows(i).Cells(8).Value = dt.Rows(i).ItemArray(7)
'DataGrid1.Rows(i).Cells(9).Value = dt.Rows(i).ItemArray(8)
DataGrid1.Rows(i).Cells(10).Value = dt.Rows(i).ItemArray(9)
DataGrid1.Rows(i).Cells(11).Value = dt.Rows(i).ItemArray(10)
Next
SQL.conn.Close()
For i = 0 To DataGrid1.RowCount - 1
DataGrid1.Rows(i).HeaderCell.Value = CStr(i + 1)
Next
End Sub
推荐答案
检查过去的答案:使用Datagridview进行分页 [
Check past answer: Paging with Datagridview[^]
这篇关于我如何分页获取datagridview VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!