Home > Article > Operation and Maintenance > How to use the XtraGrid scroll wheel to turn pages
Wheel page turning and transmission page turning are more convenient. After some discussion and thinking, I finally implemented mouse wheel page turning in XtraGrid's GridView.
I created a new component that inherits the original GridControl, and added an ImageList to the component to store some resource images. Used to achieve the effect of dynamic graphics.
Add a custom delegate parameter and enumeration. The delegate parameter is used to transfer paging information.
public class PagingEventArgs : EventArgs { public int PageSize { get; set; } public int PageIndex { get; set; } } public enum LoadState { /// <summary> /// 就绪 /// </summary> Ready, /// <summary> /// 正在读取 /// </summary> Loading, /// <summary> /// 读取完成 /// </summary> Finish }
Add the following fields in the component class
/// <summary> /// 页面大小 /// </summary> private int _int_page_size=20; /// <summary> /// 当前页索引 /// </summary> private int _int_page_index=1; /// <summary> /// 总记录数 /// </summary> private int _int_record_count; /// <summary> /// 读取状态 /// </summary> private LoadState _LodaState_state;
Add the following properties
(!IsPaging) (!IsPaging) = (value>,
)
GridView_main_view.DeleteRow(0);
this.RefreshDataSource();
} } /// <summary> /// 每次读取的行数 /// </summary> public int PageSize { get { if (!IsPaging) return 0; return _int_page_size; } set { if (!IsPaging) return ; _int_page_size = value; } } /// <summary> /// 总页数 /// </summary> private int PageCount { get { if (RecordCount % PageSize == 0) return RecordCount / PageSize; return RecordCount / PageSize + 1; } } /// <summary> /// Grid /// </summary> private GridView _GridView_main_view { get { return (GridView)this.MainView; } } /// <summary> /// 是否启用分页 /// </summary> public bool IsPaging { get; set; }
/// <summary> /// 内部使用的委托 /// </summary> private delegate void myDelegate(); /// <summary> /// 滚动翻页的委托 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public delegate void ScrollingToPageEventHandler(object sender, PagingEventArgs e); /// <summary> /// 滚动翻页的事件 /// </summary> public event ScrollingToPageEventHandler OnScrollingToPage;The following are some of the controls Settings can be changed according to personal preference.
/// <summary> /// 设置分页栏 /// </summary> private void InitEmbeddedNavigator() {this.EmbeddedNavigator.CustomButtons.AddRange(new DevExpress.XtraEditors.NavigatorCustomButton[] { new DevExpress.XtraEditors.NavigatorCustomButton(-1, -1, true, false, "", null)}); this.EmbeddedNavigator.TextStringFormat = " 当前 {1} 行数据 "; this.UseEmbeddedNavigator = true; } /// <summary> /// 设置gridView /// </summary> private void InitGridView() { _GridView_main_view.TopRowChanged += new EventHandler(gridView_TopRowChanged); }Register the following method for the event of the control
private void gridControl_Load(object sender, EventArgs e) { if (IsPaging) { _LodaState_state = LoadState.Ready; InitEmbeddedNavigator(); InitGridView(); } } private void gridView_TopRowChanged(object sender, EventArgs e) { lock (this) { if ( _int_page_index > PageCount || _LodaState_state != LoadState.Ready) return; } //检查是否到达底部 if (_GridView_main_view.IsRowVisible(_GridView_main_view.RowCount - 1) == RowVisibleState.Visible|| _int_page_index==1) { lock (this)//设置成开始读取状态 { _LodaState_state = LoadState.Loading; } Thread thread_load_data = new Thread(new ThreadStart(LoadData)); Thread thread_change_text = new Thread(new ThreadStart(ChangeLoadingImage)); thread_change_text.Start(); thread_load_data.Start(); } }The TopRowChanged event will be triggered when the first row of the grid changes, similar to the Scroll event of the scroll bar. Two threads are opened here, the first thread is used to read data, and the second thread is used to implement dynamic graphics. The methods called by both threads are below
top_row_index = focus_index = (== (OnScrollingToPage == Exception(= = = (.Parent.Invoke( myDelegate(== (= LoadState.Finish; p_w_picpath_index = (.Parent.InvokeRequired).Parent.Invoke( myDelegate(.EmbeddedNavigator.Buttons.CustomButtons[].Visible = () ( (_LodaState_state != LoadState.Loading) (p_w_picpath_index == = ++ ( .Parent.Invoke( myDelegate(.EmbeddedNavigator.Buttons.CustomButtons[].ImageIndex = (.Parent.InvokeRequired).Parent.Invoke( myDelegate(.EmbeddedNavigator.Buttons.CustomButtons[].Visible = (= ++However, there is a problem with this code. When the data source bound to the GridControl has children of the same instance, the TopRowChanged event will be triggered continuously as the RefreshData method is called. The exact reason is not yet clear. The solution to this problem is to either remove the same instance sub-items on the data source or not call the RefreshData method.
The above is the detailed content of How to use the XtraGrid scroll wheel to turn pages. For more information, please follow other related articles on the PHP Chinese website!