需求如上圖所示:界面中有3個數據控件。
第一個數據控件中有兩列需要進行單元格編輯。

1 private void gvforecast_ShowingEditor(object sender, CancelEventArgs e) 2 { 3 GridView view = sender as GridView; 4 string fieldName = view.FocusedColumn.FieldName; 5 //如果列名是"Forecast"且Locked列值不等於N,則不可以編輯 6 if ((fieldName == "Forecast"|| fieldName=="WForecast") && !DisableEditCell(view, view.FocusedRowHandle)) 7 { 8 e.Cancel = true; 9 } 10 }
第二個數據控件和第三個控件為明細數據。
操作要求:進行單元格編輯時同時加載明細數據。
正常操作情況在第一個Grid單元格編輯事件中同時加載數據,但是會出現界面假死狀態。只有明細數據加載完成后,單元格才能獲取到光標。這樣用戶體驗度不好,為了提高用戶體驗我想到的解決方案是異步加載明細數據。
關鍵字:async、await、Task<T>、Task。至於這幾個關鍵字的原理不做解釋,直接上代碼

1 private Task<DataSet> GetData(string week,string branch,string pol,string schCode) 2 { 3 return Task.Run<DataSet>(() => 4 { 5 return FeederForecastBiz.GetDataSet(week, branch, pol, schCode); 6 } 7 ); 8 }

1 private async void LoadData(string week, string branch, string pol, string schCode) 2 { 3 DataSet ds = await GetData(week, branch, pol, schCode); 4 if (ds != null) 5 { 6 DataTable tableTeu = ds.Tables["tableTeu"]; 7 DataTable tableBk = ds.Tables["tableBk"]; 8 if (tableTeu != null && tableTeu.Rows.Count > 0) 9 { 10 this.gdReceiveTeu.DataSource = tableTeu; 11 InitGridColumn(gvReceiveTeu); 12 } 13 if (tableBk != null && tableBk.Rows.Count > 0) 14 { 15 this.gdBk.DataSource = tableBk; 16 InitGridColumn(gvBk); 17 } 18 } 19 }
關鍵代碼如上,然后我們直接調用加載數據方法即可。卡頓現象消除
---------------------------------------------------
Devexpres GridControl列自適應寬度,同時開啟滾動條
設置:
AutoColumnWith=false
HorzScrollVisibility:Always

1 private void InitGridColumn(GridView gv) 2 { 3 for (int i = 0; i < gv.Columns.Count; i++) 4 { 5 gv.BestFitColumns(); 6 gv.Columns[i].BestFit(); 7 } 8 }