1. 数据部分的DataGridView,不带任何滚动框
2.合计部分的DataGridView,带有横向滚动框
3.在画面上添加一个纵向滚动框
实现的主要思路就是用合计行的横向滚动框控制两个DataGridView的横向滚动,右侧的纵向滚动狂控制数据部分的DataGridView,效果看起来就是合计行始终显示。
该例实现了合计行的自动计算,取数据的部分是用程序做的DataTable
DataGridViewSumRow类
Public
Class DataGridViewSumRow
Private dt As DataTable
Dim dtSum As DataTable
Private ROW_HEIGHT As Integer = 21 ''行高
Private Sub DataGridViewSumRow_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
VScrollBar1.Visible = False
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
dt = GetData()
Me.DataGridView1.DataSource = dt
Me.DataGridView1.RowTemplate.Height = ROW_HEIGHT
GetSumData()
''行数超过当前页显示时显示纵向滚动条
If dt.Rows.Count > 13 Then
VScrollBar1.Visible = True
''总长度为 (所有行数 - 画面一页显示行数) × 行高
VScrollBar1.Maximum = (Me.DataGridView1.Rows.Count - Me.DataGridView1.DisplayedRowCount(False)) * ROW_HEIGHT
VScrollBar1.Minimum = 0
VScrollBar1.SmallChange = 21
VScrollBar1.LargeChange = 50
End If
End Sub
''' <summary>
''' 合计取得设定
''' </summary>
''' <remarks></remarks>
Private Sub GetSumData()
Dim dr As DataRow
dtSum = New DataTable("TEST")
dtSum = dt.Clone
Dim rdm As Random = New Random
dr = dtSum.NewRow()
dr(0) = "合计"
For i As Integer = 1 To dt.Columns.Count - 1
dr(i) = dt.Compute("Sum(" + dt.Columns(i).ColumnName + ")", "true")
Next
dtSum.Rows.Add(dr)
Me.DataGridViewSum.DataSource = dtSum
Me.DataGridViewSum.Rows(0).DefaultCellStyle.BackColor = Color.Brown
Me.DataGridViewSum.ReadOnly = True
Me.DataGridViewSum.SelectionMode = DataGridViewSelectionMode.FullRowSelect
End Sub
''' <summary>
''' 数据取得
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Function GetData() As DataTable
Dim dt As DataTable
Dim dr As DataRow
dt = New DataTable("TEST")
dt.Columns.Add(New DataColumn("号码", GetType(String)))
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)))
dt.Columns.Add(New DataColumn("数量8", GetType(Integer)))
dt.Columns.Add(New DataColumn("数量9", GetType(Integer)))
Dim rdm As Random = New Random
For i As Integer = 10 To 80
dr = dt.NewRow()
dr(0) = "00" & i.ToString
For j As Integer = 1 To 9
dr(j) = rdm.Next(1000, 5000)
Next
dt.Rows.Add(dr)
Next
Return dt
End Function
''' <summary>
''' 纵滚动条事件
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub VScrollBar1_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles VScrollBar1.Scroll
'Debug.WriteLine(e.NewValue.ToString)
Me.DataGridView1.FirstDisplayedScrollingRowIndex = e.NewValue ROW_HEIGHT
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
''' <summary>
''' 合计DataGridView的滚动条事件
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub DataGridViewSum_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles DataGridViewSum.Scroll
Me.DataGridView1.HorizontalScrollingOffset = e.NewValue
End Sub
''' <summary>
''' 数据变更后重新合计
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
If e.ColumnIndex <= 0 Then
Exit Sub
End If
''DetaGridView中数据变化后重新合计
Me.DataGridViewSum(e.ColumnIndex, 0).Value = dt.Compute("Sum(" + dt.Columns(e.ColumnIndex).ColumnName + ")", "true")
End Sub
''' <summary>
''' 响应鼠标滚轴事件
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub DataGridViewSumRow_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel
If VScrollBar1.Visible = False Then
Exit Sub
End If
If VScrollBar1.Value - ROW_HEIGHT < 0 And e.Delta > 0 Then
VScrollBar1.Value = VScrollBar1.Minimum
Exit Sub
End If
If VScrollBar1.Value + ROW_HEIGHT > VScrollBar1.Maximum And e.Delta < 0 Then
VScrollBar1.Value = VScrollBar1.Maximum
Exit Sub
End If
Try
VScrollBar1.Value -= CInt(e.Delta / Math.Abs(e.Delta) * ROW_HEIGHT)
Catch ex As Exception
Debug.WriteLine("###########################")
Debug.WriteLine("VScrollBar1.Value = " & VScrollBar1.Value)
Debug.WriteLine("e.Delta = " & e.Delta)
Debug.WriteLine("###########################")
End Try
Me.DataGridView1.FirstDisplayedScrollingRowIndex = VScrollBar1.Value ROW_HEIGHT
End Sub
End Class
Private dt As DataTable
Dim dtSum As DataTable
Private ROW_HEIGHT As Integer = 21 ''行高
Private Sub DataGridViewSumRow_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
VScrollBar1.Visible = False
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
dt = GetData()
Me.DataGridView1.DataSource = dt
Me.DataGridView1.RowTemplate.Height = ROW_HEIGHT
GetSumData()
''行数超过当前页显示时显示纵向滚动条
If dt.Rows.Count > 13 Then
VScrollBar1.Visible = True
''总长度为 (所有行数 - 画面一页显示行数) × 行高
VScrollBar1.Maximum = (Me.DataGridView1.Rows.Count - Me.DataGridView1.DisplayedRowCount(False)) * ROW_HEIGHT
VScrollBar1.Minimum = 0
VScrollBar1.SmallChange = 21
VScrollBar1.LargeChange = 50
End If
End Sub
''' <summary>
''' 合计取得设定
''' </summary>
''' <remarks></remarks>
Private Sub GetSumData()
Dim dr As DataRow
dtSum = New DataTable("TEST")
dtSum = dt.Clone
Dim rdm As Random = New Random
dr = dtSum.NewRow()
dr(0) = "合计"
For i As Integer = 1 To dt.Columns.Count - 1
dr(i) = dt.Compute("Sum(" + dt.Columns(i).ColumnName + ")", "true")
Next
dtSum.Rows.Add(dr)
Me.DataGridViewSum.DataSource = dtSum
Me.DataGridViewSum.Rows(0).DefaultCellStyle.BackColor = Color.Brown
Me.DataGridViewSum.ReadOnly = True
Me.DataGridViewSum.SelectionMode = DataGridViewSelectionMode.FullRowSelect
End Sub
''' <summary>
''' 数据取得
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Function GetData() As DataTable
Dim dt As DataTable
Dim dr As DataRow
dt = New DataTable("TEST")
dt.Columns.Add(New DataColumn("号码", GetType(String)))
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)))
dt.Columns.Add(New DataColumn("数量8", GetType(Integer)))
dt.Columns.Add(New DataColumn("数量9", GetType(Integer)))
Dim rdm As Random = New Random
For i As Integer = 10 To 80
dr = dt.NewRow()
dr(0) = "00" & i.ToString
For j As Integer = 1 To 9
dr(j) = rdm.Next(1000, 5000)
Next
dt.Rows.Add(dr)
Next
Return dt
End Function
''' <summary>
''' 纵滚动条事件
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub VScrollBar1_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles VScrollBar1.Scroll
'Debug.WriteLine(e.NewValue.ToString)
Me.DataGridView1.FirstDisplayedScrollingRowIndex = e.NewValue ROW_HEIGHT
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
''' <summary>
''' 合计DataGridView的滚动条事件
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub DataGridViewSum_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles DataGridViewSum.Scroll
Me.DataGridView1.HorizontalScrollingOffset = e.NewValue
End Sub
''' <summary>
''' 数据变更后重新合计
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
If e.ColumnIndex <= 0 Then
Exit Sub
End If
''DetaGridView中数据变化后重新合计
Me.DataGridViewSum(e.ColumnIndex, 0).Value = dt.Compute("Sum(" + dt.Columns(e.ColumnIndex).ColumnName + ")", "true")
End Sub
''' <summary>
''' 响应鼠标滚轴事件
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub DataGridViewSumRow_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel
If VScrollBar1.Visible = False Then
Exit Sub
End If
If VScrollBar1.Value - ROW_HEIGHT < 0 And e.Delta > 0 Then
VScrollBar1.Value = VScrollBar1.Minimum
Exit Sub
End If
If VScrollBar1.Value + ROW_HEIGHT > VScrollBar1.Maximum And e.Delta < 0 Then
VScrollBar1.Value = VScrollBar1.Maximum
Exit Sub
End If
Try
VScrollBar1.Value -= CInt(e.Delta / Math.Abs(e.Delta) * ROW_HEIGHT)
Catch ex As Exception
Debug.WriteLine("###########################")
Debug.WriteLine("VScrollBar1.Value = " & VScrollBar1.Value)
Debug.WriteLine("e.Delta = " & e.Delta)
Debug.WriteLine("###########################")
End Try
Me.DataGridView1.FirstDisplayedScrollingRowIndex = VScrollBar1.Value ROW_HEIGHT
End Sub
End Class
效果如下