1.数据部分的DataGridView,不带任何滚动框
2.合计部分的DataGridView,带有横向滚动框
3.在画面上添加一个纵向滚动框
实现的主要思路就是用合计行的横向滚动框控制两个DataGridView的横向滚动,右侧的纵向滚动狂控制数据部分的DataGridView,效果看起来就是合计行始终显示。
该例实现了合计行的自动计算,取数据的部分是用程序做的DataTable
DataGridViewSumRow类
PublicClassDataGridViewSumRow
PrivatedtAsDataTable
DimdtSumAsDataTable
PrivateROW_HEIGHTAsInteger=21''行高
PrivateSubDataGridViewSumRow_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.Load
VScrollBar1.Visible=False
EndSub
PrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.Click
dt=GetData()
Me.DataGridView1.DataSource=dt
Me.DataGridView1.RowTemplate.Height=ROW_HEIGHT
GetSumData()
''行数超过当前页显示时显示纵向滚动条
Ifdt.Rows.Count>13Then
VScrollBar1.Visible=True
''总长度为(所有行数-画面一页显示行数)×行高
VScrollBar1.Maximum=(Me.DataGridView1.Rows.Count-Me.DataGridView1.DisplayedRowCount(False))*ROW_HEIGHT
VScrollBar1.Minimum=0
VScrollBar1.SmallChange=21
VScrollBar1.LargeChange=50
EndIf
EndSub
'''<summary>
'''合计取得设定
'''</summary>
'''<remarks></remarks>
PrivateSubGetSumData()
DimdrAsDataRow
dtSum=NewDataTable("TEST")
dtSum=dt.Clone
DimrdmAsRandom=NewRandom
dr=dtSum.NewRow()
dr(0)="合计"
ForiAsInteger=1Todt.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
EndSub
'''<summary>
'''数据取得
'''</summary>
'''<returns></returns>
'''<remarks></remarks>
PublicFunctionGetData()AsDataTable
DimdtAsDataTable
DimdrAsDataRow
dt=NewDataTable("TEST")
dt.Columns.Add(NewDataColumn("号码",GetType(String)))
dt.Columns.Add(NewDataColumn("数量1",GetType(Integer)))
dt.Columns.Add(NewDataColumn("数量2",GetType(Integer)))
dt.Columns.Add(NewDataColumn("数量3",GetType(Integer)))
dt.Columns.Add(NewDataColumn("数量4",GetType(Integer)))
dt.Columns.Add(NewDataColumn("数量5",GetType(Integer)))
dt.Columns.Add(NewDataColumn("数量6",GetType(Integer)))
dt.Columns.Add(NewDataColumn("数量7",GetType(Integer)))
dt.Columns.Add(NewDataColumn("数量8",GetType(Integer)))
dt.Columns.Add(NewDataColumn("数量9",GetType(Integer)))
DimrdmAsRandom=NewRandom
ForiAsInteger=10To80
dr=dt.NewRow()
dr(0)="00"&i.ToString
ForjAsInteger=1To9
dr(j)=rdm.Next(1000,5000)
Next
dt.Rows.Add(dr)
Next
Returndt
EndFunction
'''<summary>
'''纵滚动条事件
'''</summary>
'''<paramname="sender"></param>
'''<paramname="e"></param>
'''<remarks></remarks>
PrivateSubVScrollBar1_Scroll(ByValsenderAsObject,ByValeAsSystem.Windows.Forms.ScrollEventArgs)HandlesVScrollBar1.Scroll
'Debug.WriteLine(e.NewValue.ToString)
Me.DataGridView1.FirstDisplayedScrollingRowIndex=e.NewValueROW_HEIGHT
EndSub
PrivateSubButton2_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton2.Click
Me.Close()
EndSub
'''<summary>
'''合计DataGridView的滚动条事件
'''</summary>
'''<paramname="sender"></param>
'''<paramname="e"></param>
'''<remarks></remarks>
PrivateSubDataGridViewSum_Scroll(ByValsenderAsObject,ByValeAsSystem.Windows.Forms.ScrollEventArgs)HandlesDataGridViewSum.Scroll
Me.DataGridView1.HorizontalScrollingOffset=e.NewValue
EndSub
'''<summary>
'''数据变更后重新合计
'''</summary>
'''<paramname="sender"></param>
'''<paramname="e"></param>
'''<remarks></remarks>
PrivateSubDataGridView1_CellValueChanged(ByValsenderAsObject,ByValeAsSystem.Windows.Forms.DataGridViewCellEventArgs)HandlesDataGridView1.CellValueChanged
Ife.ColumnIndex<=0Then
ExitSub
EndIf
''DetaGridView中数据变化后重新合计
Me.DataGridViewSum(e.ColumnIndex,0).Value=dt.Compute("Sum("+dt.Columns(e.ColumnIndex).ColumnName+")","true")
EndSub
'''<summary>
'''响应鼠标滚轴事件
'''</summary>
'''<paramname="sender"></param>
'''<paramname="e"></param>
'''<remarks></remarks>
PrivateSubDataGridViewSumRow_MouseWheel(ByValsenderAsObject,ByValeAsSystem.Windows.Forms.MouseEventArgs)HandlesMe.MouseWheel
IfVScrollBar1.Visible=FalseThen
ExitSub
EndIf
IfVScrollBar1.Value-ROW_HEIGHT<0Ande.Delta>0Then
VScrollBar1.Value=VScrollBar1.Minimum
ExitSub
EndIf
IfVScrollBar1.Value+ROW_HEIGHT>VScrollBar1.MaximumAnde.Delta<0Then
VScrollBar1.Value=VScrollBar1.Maximum
ExitSub
EndIf
Try
VScrollBar1.Value-=CInt(e.Delta/Math.Abs(e.Delta)*ROW_HEIGHT)
CatchexAsException
Debug.WriteLine("###########################")
Debug.WriteLine("VScrollBar1.Value="&VScrollBar1.Value)
Debug.WriteLine("e.Delta="&e.Delta)
Debug.WriteLine("###########################")
EndTry
Me.DataGridView1.FirstDisplayedScrollingRowIndex=VScrollBar1.ValueROW_HEIGHT
EndSub
EndClass
PrivatedtAsDataTable
DimdtSumAsDataTable
PrivateROW_HEIGHTAsInteger=21''行高
PrivateSubDataGridViewSumRow_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.Load
VScrollBar1.Visible=False
EndSub
PrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.Click
dt=GetData()
Me.DataGridView1.DataSource=dt
Me.DataGridView1.RowTemplate.Height=ROW_HEIGHT
GetSumData()
''行数超过当前页显示时显示纵向滚动条
Ifdt.Rows.Count>13Then
VScrollBar1.Visible=True
''总长度为(所有行数-画面一页显示行数)×行高
VScrollBar1.Maximum=(Me.DataGridView1.Rows.Count-Me.DataGridView1.DisplayedRowCount(False))*ROW_HEIGHT
VScrollBar1.Minimum=0
VScrollBar1.SmallChange=21
VScrollBar1.LargeChange=50
EndIf
EndSub
'''<summary>
'''合计取得设定
'''</summary>
'''<remarks></remarks>
PrivateSubGetSumData()
DimdrAsDataRow
dtSum=NewDataTable("TEST")
dtSum=dt.Clone
DimrdmAsRandom=NewRandom
dr=dtSum.NewRow()
dr(0)="合计"
ForiAsInteger=1Todt.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
EndSub
'''<summary>
'''数据取得
'''</summary>
'''<returns></returns>
'''<remarks></remarks>
PublicFunctionGetData()AsDataTable
DimdtAsDataTable
DimdrAsDataRow
dt=NewDataTable("TEST")
dt.Columns.Add(NewDataColumn("号码",GetType(String)))
dt.Columns.Add(NewDataColumn("数量1",GetType(Integer)))
dt.Columns.Add(NewDataColumn("数量2",GetType(Integer)))
dt.Columns.Add(NewDataColumn("数量3",GetType(Integer)))
dt.Columns.Add(NewDataColumn("数量4",GetType(Integer)))
dt.Columns.Add(NewDataColumn("数量5",GetType(Integer)))
dt.Columns.Add(NewDataColumn("数量6",GetType(Integer)))
dt.Columns.Add(NewDataColumn("数量7",GetType(Integer)))
dt.Columns.Add(NewDataColumn("数量8",GetType(Integer)))
dt.Columns.Add(NewDataColumn("数量9",GetType(Integer)))
DimrdmAsRandom=NewRandom
ForiAsInteger=10To80
dr=dt.NewRow()
dr(0)="00"&i.ToString
ForjAsInteger=1To9
dr(j)=rdm.Next(1000,5000)
Next
dt.Rows.Add(dr)
Next
Returndt
EndFunction
'''<summary>
'''纵滚动条事件
'''</summary>
'''<paramname="sender"></param>
'''<paramname="e"></param>
'''<remarks></remarks>
PrivateSubVScrollBar1_Scroll(ByValsenderAsObject,ByValeAsSystem.Windows.Forms.ScrollEventArgs)HandlesVScrollBar1.Scroll
'Debug.WriteLine(e.NewValue.ToString)
Me.DataGridView1.FirstDisplayedScrollingRowIndex=e.NewValueROW_HEIGHT
EndSub
PrivateSubButton2_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton2.Click
Me.Close()
EndSub
'''<summary>
'''合计DataGridView的滚动条事件
'''</summary>
'''<paramname="sender"></param>
'''<paramname="e"></param>
'''<remarks></remarks>
PrivateSubDataGridViewSum_Scroll(ByValsenderAsObject,ByValeAsSystem.Windows.Forms.ScrollEventArgs)HandlesDataGridViewSum.Scroll
Me.DataGridView1.HorizontalScrollingOffset=e.NewValue
EndSub
'''<summary>
'''数据变更后重新合计
'''</summary>
'''<paramname="sender"></param>
'''<paramname="e"></param>
'''<remarks></remarks>
PrivateSubDataGridView1_CellValueChanged(ByValsenderAsObject,ByValeAsSystem.Windows.Forms.DataGridViewCellEventArgs)HandlesDataGridView1.CellValueChanged
Ife.ColumnIndex<=0Then
ExitSub
EndIf
''DetaGridView中数据变化后重新合计
Me.DataGridViewSum(e.ColumnIndex,0).Value=dt.Compute("Sum("+dt.Columns(e.ColumnIndex).ColumnName+")","true")
EndSub
'''<summary>
'''响应鼠标滚轴事件
'''</summary>
'''<paramname="sender"></param>
'''<paramname="e"></param>
'''<remarks></remarks>
PrivateSubDataGridViewSumRow_MouseWheel(ByValsenderAsObject,ByValeAsSystem.Windows.Forms.MouseEventArgs)HandlesMe.MouseWheel
IfVScrollBar1.Visible=FalseThen
ExitSub
EndIf
IfVScrollBar1.Value-ROW_HEIGHT<0Ande.Delta>0Then
VScrollBar1.Value=VScrollBar1.Minimum
ExitSub
EndIf
IfVScrollBar1.Value+ROW_HEIGHT>VScrollBar1.MaximumAnde.Delta<0Then
VScrollBar1.Value=VScrollBar1.Maximum
ExitSub
EndIf
Try
VScrollBar1.Value-=CInt(e.Delta/Math.Abs(e.Delta)*ROW_HEIGHT)
CatchexAsException
Debug.WriteLine("###########################")
Debug.WriteLine("VScrollBar1.Value="&VScrollBar1.Value)
Debug.WriteLine("e.Delta="&e.Delta)
Debug.WriteLine("###########################")
EndTry
Me.DataGridView1.FirstDisplayedScrollingRowIndex=VScrollBar1.ValueROW_HEIGHT
EndSub
EndClass
效果如下