Windows Develop Bookmark and Share   
 index > Windows Forms General > DataGridView: VerticalScrollingOffset readonly while HorizontalScrollingOffset is not
 

DataGridView: VerticalScrollingOffset readonly while HorizontalScrollingOffset is not

Any explanation why the VerticalScrollingOffset property of the DataGridView control is readonly while it's counterpart, HorizontalScrollingOffset, is not?

I want to synchronize the vertical scrolling of two DataGridViews. I realize there are the FirstXXX properties, but that doesn't offer the granular scrolling that I would need if the user decides to drag the scrollbar.

Sync'ing the horizontal scroll using HorizontalScrollingOffset works well, I wish I could do the same with vertical.

~Jayson

JaysonGo  Wednesday, March 21, 2007 2:21 PM
You can handle the scroll event to set the FirstDisplayRowIndex property to sync the datagridviews. Below is my sample

Sample4

public partial class DgvSyncScroll : Form

{

public DgvSyncScroll()

{

InitializeComponent();

}

private void DgvSyncScroll_Load(object sender, EventArgs e)

{

DataTable dt2 = new DataTable();

dt2.Columns.Add("OrderId", typeof(int));

dt2.Columns.Add("ProdID", typeof(int));

dt2.Columns.Add("Quantity", typeof(int));

for (int j = 0; j < 22; j++)

{

dt2.Rows.Add(j, j + 1, j + 2);

}

this.dataGridView1.DataSource = dt2;

this.dataGridView2.DataSource = dt2;

this.dataGridView1.Scroll += new ScrollEventHandler(dataGridView1_Scroll);

this.dataGridView2.Scroll += new ScrollEventHandler(dataGridView2_Scroll);

}

void dataGridView2_Scroll(object sender, ScrollEventArgs e)

{

this.dataGridView1.FirstDisplayedScrollingRowIndex = this.dataGridView2.FirstDisplayedScrollingRowIndex;

}

void dataGridView1_Scroll(object sender, ScrollEventArgs e)

{

this.dataGridView2.FirstDisplayedScrollingRowIndex = this.dataGridView1.FirstDisplayedScrollingRowIndex;

}

}

Zhi-Xin Ye  Friday, March 23, 2007 6:23 AM
You can handle the scroll event to set the FirstDisplayRowIndex property to sync the datagridviews. Below is my sample

Sample4

public partial class DgvSyncScroll : Form

{

public DgvSyncScroll()

{

InitializeComponent();

}

private void DgvSyncScroll_Load(object sender, EventArgs e)

{

DataTable dt2 = new DataTable();

dt2.Columns.Add("OrderId", typeof(int));

dt2.Columns.Add("ProdID", typeof(int));

dt2.Columns.Add("Quantity", typeof(int));

for (int j = 0; j < 22; j++)

{

dt2.Rows.Add(j, j + 1, j + 2);

}

this.dataGridView1.DataSource = dt2;

this.dataGridView2.DataSource = dt2;

this.dataGridView1.Scroll += new ScrollEventHandler(dataGridView1_Scroll);

this.dataGridView2.Scroll += new ScrollEventHandler(dataGridView2_Scroll);

}

void dataGridView2_Scroll(object sender, ScrollEventArgs e)

{

this.dataGridView1.FirstDisplayedScrollingRowIndex = this.dataGridView2.FirstDisplayedScrollingRowIndex;

}

void dataGridView1_Scroll(object sender, ScrollEventArgs e)

{

this.dataGridView2.FirstDisplayedScrollingRowIndex = this.dataGridView1.FirstDisplayedScrollingRowIndex;

}

}

Zhi-Xin Ye  Friday, March 23, 2007 6:23 AM

I did end up doing something like that.. although it doesn't give you that granular (per pixel) scrolling Sad

Thanks.

JaysonGo  Tuesday, March 27, 2007 8:39 PM

You can use google to search for other answers

Custom Search

More Threads

• calling the 'on_load' event or re-setting the form ?
• Toolbar Docked to Top Covering Parts of the Form
• Popup menu...
• Can’t separate all GUI code (UITypeEditor) from Business layer
• Resizing Drag & drop control at run time
• Resizing images drawn in picturebox...
• looping thru a listbox to match the text to a selected string
• crystalreports
• Report Viewer Memory Leak Take 2.0
• Calculate values and populate listview correctly