C#dataGridView 滚动条 同步

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#dataGridView 滚动条 同步相关的知识,希望对你有一定的参考价值。

我想用滚动条同时控制两个datagridview,拉一个滚动条的时候,另外的一个datagridview也相应移动,求大神们帮帮吧...

参考技术A private void Form1_Load(object sender, EventArgs e)

DataTable dt = new DataTable();
dt.Columns.Add("col1");
dt.Columns.Add("col2");
dt.Columns.Add("col3");
for (int i = 0; i < 100; i++)

dt.Rows.Add(new object[] i,i+1,i+2);

this.dataGridView1.DataSource = dt;
this.dataGridView2.DataSource = dt;


private void dataGridView1_Scroll(object sender, ScrollEventArgs e)

dataGridView2.FirstDisplayedScrollingRowIndex = dataGridView1.FirstDisplayedScrollingRowIndex;
dataGridView2.HorizontalScrollingOffset = dataGridView1.HorizontalScrollingOffset;


private void dataGridView2_Scroll(object sender, ScrollEventArgs e)

dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView2.FirstDisplayedScrollingRowIndex;
dataGridView1.HorizontalScrollingOffset = dataGridView2.HorizontalScrollingOffset;
本回答被提问者采纳

C#两个datagridview分别绑定了相同结构的内容不同的DATATABLE,求两个datagridview的横纵滚动条同步滚动

这么写就行了

类:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Reflection;
namespace Test

public class DataGridViewEx : DataGridView

private Type type = typeof(DataGridViewEx);
private PropertyInfo horPropertyInfo;
private PropertyInfo verPropertyInfo;
public DataGridViewEx ScrollDataGridViewEx get; set;
public new ScrollBar VerticalScrollBar

get

return base.VerticalScrollBar;


public new ScrollBar HorizontalScrollBar

get

return base.HorizontalScrollBar;


public DataGridViewEx()

horPropertyInfo = type.GetProperty("HorizontalOffset", BindingFlags.NonPublic | BindingFlags.Instance);
verPropertyInfo = type.GetProperty("VerticalOffset", BindingFlags.NonPublic | BindingFlags.Instance);

protected override void OnScroll(ScrollEventArgs e)

if (ScrollDataGridViewEx != null)

if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll &&
ScrollDataGridViewEx.HorizontalScrollBar.Visible)

ScrollDataGridViewEx.HorizontalScrollBar.Value = this.HorizontalScrollBar.Value;
horPropertyInfo.SetValue(ScrollDataGridViewEx, this.HorizontalScrollBar.Value, null);

if (e.ScrollOrientation == ScrollOrientation.VerticalScroll &&
ScrollDataGridViewEx.VerticalScrollBar.Visible)

ScrollDataGridViewEx.VerticalScrollBar.Value = this.VerticalScrollBar.Value;
verPropertyInfo.SetValue(ScrollDataGridViewEx, this.VerticalScrollBar.Value, null);


base.OnScroll(e);


使用:

private void Form1_Load(object sender, EventArgs e)

dataGridViewEx1.ScrollDataGridViewEx = dataGridViewEx2;
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("A"));
dt.Columns.Add(new DataColumn("B"));
dt.Columns.Add(new DataColumn("C"));
dt.Columns.Add(new DataColumn("D"));
DataTable dt2 = dt.Clone();
for (int i = 0; i < 20; i++)

dt.Rows.Add(i, i + 1, i + 2, i + 3);
dt2.Rows.Add(i, i + 10, i + 20, i + 30);

dataGridViewEx1.DataSource = dt;
dataGridViewEx2.DataSource = dt2;

参考技术A 在滚动事件里写代码控制另一台dataGridView的滚动条追问

滚动条的坐标位置如何获取?能给出滚动事件的代码吗?

追答

private void dataGridView1_Scroll(object sender, ScrollEventArgs e)

dataGridView2.FirstDisplayedScrollingColumnIndex = dataGridView1.FirstDisplayedScrollingColumnIndex;

追问

我试了,但是dataGridView1.FirstDisplayedScrollingColumnIndex永远是“0”啊?这个是为什么?

追答

你是横向滚动还是纵向滚动啊

追问

横向跟纵向都要!

以上是关于C#dataGridView 滚动条 同步的主要内容,如果未能解决你的问题,请参考以下文章

c# 多线程往datagridview会造成假死,滚动条无法滚动

c#winform 多线程绑定datagridview会造成假死,滚动条无法滚动,用委托怎么做

c# winform splitcontainer中datagridview滚动条不能显示,求解答~

C# datagridview 手动加水平滚动条

C# winform程序中 如何获取和设置dataGridView1的垂直滚动条当前位置?

c# datagridview 滚动条问题