您的位置:首页 > 其它

DataGridView实现分页显示数据

2007-06-26 08:42 453 查看
DataGridView实现分页显示数据

实现数据的分页,指定页数显示,列的冻结,显示行号,左上角表头的设置




Public Class ViewByPageClass ViewByPage




Private PAGE_VIEW_COUNT As Int32 = 10 ''页最大显示行数


Private p_Index As Int32 = 0 ''当前页数


Private p_MaxCount As Int32 = 0 ''最大页数


Private dt As DataTable = Nothing ''全部DataTable








读取数据#Region "读取数据"






Private Sub ReadData_Click()Sub ReadData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReadData.Click


dt = GetData()




'Me.DataGridView1.DataSource = dt




''只读


Me.DataGridView1.ReadOnly = True




''列宽自动调整


Me.DataGridView1.AutoResizeColumns()




''行表头自动调整


Me.DataGridView1.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllHeaders)




''不允许手动调整行列高度


DataGridView1.AllowUserToResizeColumns = False


DataGridView1.AllowUserToResizeRows = False




''选择模式 整行选择


Me.DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect




''滚动框 水平


Me.DataGridView1.ScrollBars = ScrollBars.Both




''左上角表头设置


Me.DataGridView1.TopLeftHeaderCell.Value = "行号"


Me.DataGridView1.TopLeftHeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter




''最大页和当前页设定


p_MaxCount = CInt((dt.Rows.Count - 1) / PAGE_VIEW_COUNT)


p_Index = 0




''设定当前页数据


SetCurrentInfo()




End Sub




#End Region






显示行号#Region "显示行号"






Private Sub DataGridView1_CellPainting()Sub DataGridView1_CellPainting(ByVal sender As Object, _


ByVal e As DataGridViewCellPaintingEventArgs) _


Handles DataGridView1.CellPainting




If e.ColumnIndex < 0 And e.RowIndex >= 0 Then




e.Paint(e.ClipBounds, DataGridViewPaintParts.All)




Dim indexRect As Rectangle = e.CellBounds


indexRect.Inflate(-2, -2)


TextRenderer.DrawText(e.Graphics, _


(e.RowIndex + 1 + p_Index * PAGE_VIEW_COUNT).ToString(), _


e.CellStyle.Font, _


indexRect, _


e.CellStyle.ForeColor, _


TextFormatFlags.Right Or TextFormatFlags.VerticalCenter)


e.Handled = True


End If


End Sub




#End Region






取得数据#Region "取得数据"






Public Function GetData()Function GetData() As DataTable




Dim dt As DataTable


Dim dr As DataRow




dt = New DataTable("TEST")


dt.Columns.Add(New DataColumn("数量1", GetType(Integer)))


dt.Columns.Add(New DataColumn("数量2", GetType(Integer)))


dt.Columns.Add(New DataColumn("数量3", GetType(Integer)))


dt.Columns.Add(New DataColumn("数量4", GetType(Integer)))


dt.Columns.Add(New DataColumn("数量5", GetType(Integer)))


dt.Columns.Add(New DataColumn("数量6", GetType(Integer)))


dt.Columns.Add(New DataColumn("数量7", GetType(Integer)))




Dim rdm As Random = New Random




For i As Integer = 1 To 95


dr = dt.NewRow()


dr(0) = rdm.Next(100, 500)


dr(1) = rdm.Next(100, 500)


dr(2) = rdm.Next(100, 500)


dr(3) = rdm.Next(100, 500)


dr(4) = rdm.Next(100, 500)


dr(5) = rdm.Next(100, 500)


dr(6) = rdm.Next(100, 500)


dt.Rows.Add(dr)


Next




Return dt


End Function




#End Region






前次页按钮操作#Region "前次页按钮操作"






Private Sub BtnGotoPageByNo_Click()Sub BtnGotoPageByNo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnGotoPageByNo.Click




If dt Is Nothing Then


Exit Sub


End If




If Me.TxtPageNo.Text = "" Then


MessageBox.Show("请输入页数。", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)


Exit Sub


End If




Try


p_Index = CInt(Me.TxtPageNo.Text) - 1


Catch ex As Exception


MessageBox.Show("请输入数字。", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)


Exit Sub


End Try




If p_Index > p_MaxCount Then


MessageBox.Show("请在页数范围内输入页数。", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)


Exit Sub


End If




SetCurrentInfo()




End Sub






Private Sub BtnGotoFirst_Click()Sub BtnGotoFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnGotoFirst.Click


p_Index = 0


SetCurrentInfo()


End Sub






Private Sub BtnGotoPrev_Click()Sub BtnGotoPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnGotoPrev.Click


p_Index = p_Index - 1


SetCurrentInfo()


End Sub






Private Sub BtnGotoNext_Click()Sub BtnGotoNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnGotoNext.Click


p_Index = p_Index + 1


SetCurrentInfo()


End Sub






Private Sub BtnGotoLast_Click()Sub BtnGotoLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnGotoLast.Click


p_Index = Me.DataGridView1.RowCount - 1


SetCurrentInfo()


End Sub




#End Region






设置当前页数据#Region "设置当前页数据"






Private Sub SetCurrentInfo()Sub SetCurrentInfo()




If dt Is Nothing Then


Exit Sub


End If




''当前页数


Me.LabelPage.Text = (p_Index + 1).ToString & "/" & (p_MaxCount + 1).ToString


Me.TxtPageNo.Text = (p_Index + 1).ToString




''取得当前页的临时DataTable


Dim dtTemp As New DataTable


dtTemp = dt.Clone


Dim i As Integer


For i = p_Index * PAGE_VIEW_COUNT To (p_Index + 1) * PAGE_VIEW_COUNT - 1


If i < dt.Rows.Count Then


Dim dr As DataRow = dtTemp.NewRow


dr.ItemArray = dt.Rows(i).ItemArray


dtTemp.Rows.Add(dr)


End If


Next




Me.DataGridView1.DataSource = dtTemp




''前两列冻结


Me.DataGridView1.Columns(1).Frozen = True




''按钮显示设定


Me.BtnGotoFirst.Enabled = (p_Index > 0)


Me.BtnGotoPrev.Enabled = (p_Index > 0)


Me.BtnGotoNext.Enabled = (p_Index < p_MaxCount)


Me.BtnGotoLast.Enabled = (p_Index < p_MaxCount)




End Sub




#End Region






Private Sub BtnClose_Click()Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClose.Click


Me.Close()


End Sub




End Class

效果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: