在我的窗口應用程序中,有很多帶有網格的屏幕。
我已經使用DataTable作爲網格的DataSource,並且DataTable具有一些非常大的數據集(> 50,000),如果我們一次加載所有內容,而在加載UI時得到不響應,則需要很長時間才能在屏幕上加載數據所有數據都未加載,
因此,我已使用Background Worker
在該網格中實施了增量加載。
下面是代碼:使用DataTable作爲數據源在網格中增量加載
// DoWork Event of the background Wroker.
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
try
{
while (bgstop)
{
e.Result = addNewRecord();
if (Convert.ToBoolean(e.Result) == false)
{
e.Cancel = true;
bgstop = false;
killBGWorker();
break;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
// to add/merge the records in the DataTable
private bool addNewRecord()
{
int flag = 0;
try
{
Thread.Sleep(500); //optional
DataTable tableAdd = getTableData();
if (tableAdd.Rows.Count > 0)
{
dtRecords.Merge(tableAdd); // dtRecords is the DataTable which attached to grid
flag++;
}
else
backgroundWorker1.WorkerSupportsCancellation = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
if (flag > 0)
return true;
else
return false;
}
// To get the next slot of Records from the DataBase
private DataTable getTableData()
{
DataTable dt = new DataTable();
start = nextRows * noOfRows;
stop = start + noOfRows;
dt = SQLHelper.getAllRecords(totalRows,noOfRows, start + 1, stop);
nextRows++;
return dt;
}
// kill the backgroudworker after the all data/records get loaded from database to grid/DataTable
private void killBGWorker()
{
backgroundWorker1.WorkerSupportsCancellation = true;
backgroundWorker1.CancelAsync();
}
上面代碼中得到的第一個定義的記錄(比如200)和數量後,在後臺工作啓動,並開始在槽獲取數據和合並與網格的DataSource,直到所有的數據(比如> 50,000條記錄)都會被加載到網格中。
但是仍然存在一些與UI交互的問題,用戶界面在數據庫的所有記錄都被加載到網格中之前,很多時間都沒有掛上2-3秒。
我經歷了this,但是在那個例子中使用了DataModel,但在我的情況下沒有DataModel,他們只是從DataBase中取出DataTable,現在我們不能移動到DataModel。
是否有任何其他方式通過良好的UI交互實現增量加載?
或
在當前情況下,有什麼方法可以實現IBindingList
?
是否可以接受簡單的讓用戶界面保持響應,或者是它重要的是你逐漸檢索行?用戶是否允許與部分數據集進行交互? – DrewJordan
爲了獲得良好的用戶體驗,我需要逐行檢索行。並且是允許用戶與部分數據集進行交互。 – BhushanK
用戶是否同時查看您的所有網格? –