View position in DBGrid when scrolling in Delphi

点点圈 提交于 2019-11-28 11:30:53

问题


I have a DBGrid on a form. The DBGrid has many columns, so an horizontal scroller is displayed. I scroll the DBGrid view to the right to see more columns. If I select a row, the DBGrid view is automatically reset to view the first column (As if I scroll back to the left most position).

Is there a way to prevent that?


回答1:


I assume you have goRowSelect in the grid options. This forces the selected col to be the first non-fixed column, so whenever the row changes the code to scroll the selected cell into view forces the first non-fixed column to be visible.

Since goRowSelect also effectively disables the horizontal scrolling with the keyboard I try to live without it. You can use custom drawing of the grid cells to show all cells of the current row with the proper colours for selected cells, even though only one cell is really selected. I use this also to show different colours depending on whether the grid is focused or not, similar to what a standard tree control does. For this to work properly you do however need to handle not only grid cell navigation events, but some other events too, like OnEnter and OnExit of the grid, OnActivate and OnDeactivate of the Application, and so on.




回答2:


You might be able to ask for the scrollbar position

GetScrollInfo(Self.Handle, SB_VERT, SIOld);

and use SetScrollInfo( ) to put it back. There's probably a better way. SelectedField is another way (get/set it as needed).



来源:https://stackoverflow.com/questions/335526/view-position-in-dbgrid-when-scrolling-in-delphi

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