在我的窗口应用程序中,有很多带有网格的屏幕。
我已经使用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
用户是否同时查看您的所有网格? –