将自定义水平滚动条的滚动同步到数据网格视图的滚动
syncing custom horizontal scrollbar's scroll to datagridview's scroll
我有一个数据网格视图和两个自定义滚动条 - 一个垂直滚动条和一个水平滚动条。
我正在填充 datagridview 时调整滚动条的大小
scrollBarEx1.Maximum = dataGridView1.RowCount;
scrollBarEx3.Maximum = dataGridView1.ColumnCount;
这是 datagridview1 的滚动事件
private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
{
if (e.ScrollOrientation == ScrollOrientation.VerticalScroll)
{
scrollBarEx1.Value = e.NewValue;
}
else if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
{
scrollBarEx3.Value = e.NewValue;
}
}
下面是两个滚动条滚动事件的代码
private void scrollBarEx1_Scroll(object sender, ScrollEventArgs e)
{
dataGridView1.Rows[dataGridView1.FirstDisplayedScrollingRowIndex].Height = e.NewValue;
}
private void scrollBarEx3_Scroll(object sender, ScrollEventArgs e)
{
dataGridView1.Columns[dataGridView1.FirstDisplayedScrollingColumnIndex].Width = e.NewValue;
}
但是只有垂直滚动有效。水平滚动会滚动,但网格不会随之滚动。请帮忙
当你有一个自定义卷轴时,你总是设置 maximum,largechange 和 small change 的值如下:
Horizontal Scroll
Maximum = total width
LargeChange = control width
SmallChange = 10% of total width, in this case the width of first column
Vertical Scroll
Maximum = total height
LargeChange = control height
SmallChange = 10% of total height, in this case the height of first row
因此对于水平:
//set these values probably at form load event
int totalwidth = dataGridView1.RowHeadersWidth + 1;
for( int i = 0; i < dataGridView1.Columns.Count; i++ ) {
totalwidth += dataGridView1.Columns[ i ].Width;
}
hScrollBar1.Maximum = totalwidth;
hScrollBar1.LargeChange = dataGridView1.Width;
hScrollBar1.SmallChange= dataGridView1.Columns[ 0 ].Width;
和
private void dataGridView1_Scroll( object sender, ScrollEventArgs e ) {
if( e.ScrollOrientation == ScrollOrientation.HorizontalScroll ) {
hScrollBar1.Value = e.NewValue;
}
}
private void hScrollBar1_Scroll( object sender, ScrollEventArgs e ) {
dataGridView1.HorizontalScrollingOffset = e.NewValue;
}
编辑
对于 DataGridView 中的垂直滚动,您不能按像素滚动,只能按行滚动。所以
Vertical Scroll
Maximum = total number of rows
LargeChange = number of visible rows, even a small part counts
SmallChange = 1
因此对于垂直
vScrollBar1.Maximum = dataGridView1.RowCount;
vScrollBar1.LargeChange = dataGridView1.DisplayedRowCount(true);
vScrollBar1.SmallChange = 1;
和
private void dataGridView1_Scroll( object sender, ScrollEventArgs e ) {
if( e.ScrollOrientation == ScrollOrientation.VerticalScroll ) {
vScrollBar1.Value = e.NewValue;
}
}
private void vScrollBar1_Scroll( object sender, ScrollEventArgs e ) {
dataGridView1.FirstDisplayedScrollingRowIndex = e.NewValue;
}
我有一个数据网格视图和两个自定义滚动条 - 一个垂直滚动条和一个水平滚动条。
我正在填充 datagridview 时调整滚动条的大小
scrollBarEx1.Maximum = dataGridView1.RowCount;
scrollBarEx3.Maximum = dataGridView1.ColumnCount;
这是 datagridview1 的滚动事件
private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
{
if (e.ScrollOrientation == ScrollOrientation.VerticalScroll)
{
scrollBarEx1.Value = e.NewValue;
}
else if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
{
scrollBarEx3.Value = e.NewValue;
}
}
下面是两个滚动条滚动事件的代码
private void scrollBarEx1_Scroll(object sender, ScrollEventArgs e)
{
dataGridView1.Rows[dataGridView1.FirstDisplayedScrollingRowIndex].Height = e.NewValue;
}
private void scrollBarEx3_Scroll(object sender, ScrollEventArgs e)
{
dataGridView1.Columns[dataGridView1.FirstDisplayedScrollingColumnIndex].Width = e.NewValue;
}
但是只有垂直滚动有效。水平滚动会滚动,但网格不会随之滚动。请帮忙
当你有一个自定义卷轴时,你总是设置 maximum,largechange 和 small change 的值如下:
Horizontal Scroll
Maximum = total width
LargeChange = control width
SmallChange = 10% of total width, in this case the width of first columnVertical Scroll
Maximum = total height
LargeChange = control height
SmallChange = 10% of total height, in this case the height of first row
因此对于水平:
//set these values probably at form load event
int totalwidth = dataGridView1.RowHeadersWidth + 1;
for( int i = 0; i < dataGridView1.Columns.Count; i++ ) {
totalwidth += dataGridView1.Columns[ i ].Width;
}
hScrollBar1.Maximum = totalwidth;
hScrollBar1.LargeChange = dataGridView1.Width;
hScrollBar1.SmallChange= dataGridView1.Columns[ 0 ].Width;
和
private void dataGridView1_Scroll( object sender, ScrollEventArgs e ) {
if( e.ScrollOrientation == ScrollOrientation.HorizontalScroll ) {
hScrollBar1.Value = e.NewValue;
}
}
private void hScrollBar1_Scroll( object sender, ScrollEventArgs e ) {
dataGridView1.HorizontalScrollingOffset = e.NewValue;
}
编辑
对于 DataGridView 中的垂直滚动,您不能按像素滚动,只能按行滚动。所以
Vertical Scroll
Maximum = total number of rows
LargeChange = number of visible rows, even a small part counts
SmallChange = 1
因此对于垂直
vScrollBar1.Maximum = dataGridView1.RowCount;
vScrollBar1.LargeChange = dataGridView1.DisplayedRowCount(true);
vScrollBar1.SmallChange = 1;
和
private void dataGridView1_Scroll( object sender, ScrollEventArgs e ) {
if( e.ScrollOrientation == ScrollOrientation.VerticalScroll ) {
vScrollBar1.Value = e.NewValue;
}
}
private void vScrollBar1_Scroll( object sender, ScrollEventArgs e ) {
dataGridView1.FirstDisplayedScrollingRowIndex = e.NewValue;
}