In runtime my app gets data from MSSQL server and there’s an object that pulls the data into my custom dataset.
Here’s the code:
public static void FillRegionData(int country, RegionDataTable DestinationTable)
{
DestinationTable.Clear();
using (selectRegionsListTableAdapter _taSource = new selectRegionsListTableAdapter())
{
BusStationDataSet.selectRegionsListDataTable _tblSource = _taSource.GetData(country, Settings.Default.DataLanguage);
foreach (BusStationDataSet.selectRegionsListRow row in _tblSource.Rows)
{
DestinationTable.Rows.Add(new object[] {
row.region,
row.country,
row.title });
}
}
}
Everything goes fine until foreach starts working. A bit more than 100 rows causes the whole app to hang for several seconds.
Any ideas why this code is so slow?
Is
DestinationTableperhaps data-bound at this point? SinceDataTableissues change notifications, adding lots of data while bound will cause a performance bottleneck. In many cases you can simply suspend the data-binding while you do this. Or alternatively, populate it first, and then give it to the data-binding.(the difference here is whether it redraws and refreshes the UI once per row, or once overall)