How to scroll to column in SpreadsheetGear WorkBookView control?

跟風遠走 提交于 2019-12-11 18:33:56

问题


I have to proofread about 30-35 spreadsheets every week.

I need to look at column W and the columns to the right of column W. When I initially display the spreadsheet I see columns A-P. I manually scroll over to column W and begin proofreading the data.

I click on a next button which steps to the next file. My code loads the next spreadsheet:

WorkbookView1.ActiveWorkSheet = Workbook_Obj.ActiveWorksheet.
WorkbookView1.Update()

Sometimes the horizontal scroll position remains the same and I can see column W and some of the columns to the right of column W.

But sometimes the horizontal scroll position changes to the left of column W and I can't see column W anymore.

I'd to do something like this:

' 23 = Column W
Col_Obj = WorkbookView1.Cells(0, 23)
' Or - find column by header text in row = 0.
Col_Obj - WorkbookView1.FindByText("Name")
Col_Obj.HorizontalScroll = Col_Obj.Location

Thanks, Ed


回答1:


You can set IWorksheetWindowInfo.ScrollColumn (note this property uses a zero-based index, i.e., where Column A is index 0, B is 1, and so on), which will set the leftmost column displayed in the window. Note there is a ScrollRow property as well if you need to set the row scroll position.

When a WorkbookView is involved, such as in your case, a number of additional factors can come into play as far as whether setting properties such as ScrollColumn will actually take effect on the WorkbookView (this has to do with the fact that you can attach the same worksheet to multiple WorkbookView controls and each WorkbookView can keep its own copy of these "window info" properties). Also, there are some behavioral differences in the way SpreadsheetGear 2012 and 2017 work in this regard. The below code should work in most cases and hopefully yours as well, though it may not in certain circumstances...for instance, if you are using SpreadsheetGear 2017 and attaching this worksheet to multiple WorkbookViews which I'm guessing you're not; but if so, I can update this answer to better accommodate your particular situation.

' Make local variable to the desired worksheet
Dim activeWorksheet = Workbook_Obj.ActiveWorksheet

' Reference cell to select and scroll to
Dim cellW1 = activeWorksheet.Cells("W1")

' Select the cell (this call alone can sometimes also automatically
' trigger the cell to be scrolled to as well, though it may not depending
' on the circumstances; and it may not make this cell the leftmost in the
' window...
cellW1.Select()

' ...to avoid any problems like mentioned above, explicitly set ScrollColumn 
' to the selected cell.  Set ScrollRow as well if desired.
activeWorksheet.WindowInfo.ScrollColumn = cellW1.Column

' Attach workbook *AFTER* doing the above.  For a variety of reasons I
' won't go into, attaching the worksheet before setting the above "window
' info" options may not have any effect.
WorkbookView1.ActiveWorksheet = Workbook_Obj.ActiveWorksheet


来源:https://stackoverflow.com/questions/55872252/how-to-scroll-to-column-in-spreadsheetgear-workbookview-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!