滾輪翻頁與傳動的翻頁更為方便,經過本人一番探討與琢磨終於在XtraGrid的GridView中實現了滑鼠滾輪翻頁。
我新建了一個元件繼承原本的GridControl,在元件中加入了一個ImageList,專門存放一些資源圖。用於實現動態圖的效果。
新增一個自訂委託的參數與枚舉,委託參數用來傳遞分頁的資訊。
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 }
在元件的類別裡面新增以下欄位
/// <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;
新增下列屬性
(!IsPaging) (!IsPaging) = (value>,
## while (this.MainView.DataRowCount > 0 )
GridView_main_view.DeleteRow(0);
總 ] ## }
} } /// <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;以下則是一些對控制項的設置,依照各人喜好可以有所更改。
/// <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); }為控制項的事件註冊以下方法
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(); } }TopRowChanged事件在grid的首行改變了就會觸發,類似於滾動條的Scroll事件。這裡開了兩個線程,第一個線程用於讀取數據,第二個線程用於實現動態圖。兩個執行緒呼叫的方法都在下面
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 = (= ++不過這個程式碼有點問題,當GridControl綁定的資料來源有相同實例的子項時,隨著RefreshData方法的呼叫會不停觸發TopRowChanged事件,確切的原因還沒搞清楚,解決這個問題就是要不移除資料來源上相同的實例項,要不要就不要呼叫RefreshData方法。
以上是如何進行XtraGrid滾輪翻頁的詳細內容。更多資訊請關注PHP中文網其他相關文章!