Replies
Hi,now I’m at home and I can show you the code to load data into a grid just in time using a DataSet.At first the form. It will be use a ultragrid and a button to start the data load. Add a BackgroundWorker to the code of the form and add methods to handle the events of the BackgroundWorker. Add a variable for a DataSet.public partial class Form1 : Form{private DataSet _ds;private BackgroundWorker _backgroundWorkerRowLike; In the button_Click event Add the following code:_ds = new DataSchemas.NewDataSet(); // a DataSet requires a schemadataGridView3.DataSource = _ds.Tables[0]; // use the ultragrid insteadif (_backgroundWorkerRowLike == null){_backgroundWorkerRowLike = new BackgroundWorker();_backgroundWorkerRowLike.WorkerReportsProgress = true;_backgroundWorkerRowLike.WorkerSupportsCancellation = true;_backgroundWorkerRowLike.DoWork += newDoWorkEventHandler(_backgroundWorkerRowLike_DoWork);_backgroundWorkerRowLike.ProgressChanged += new ProgressChangedEventHandler(_backgroundWorkerRowLike_ProgressChanged);_backgroundWorkerRowLike.RunWorkerCompleted += new RunWorkerCompletedEventHandler(_backgroundWorkerRowLike_RunWorkerCompleted);}if (!_backgroundWorkerRowLike.IsBusy)_backgroundWorkerRowLike.RunWorkerAsync(new SqlCommand(“SELECT * FROM Orders”)); The working method of the BackgroundWorker looks like this:private void _backgroundWorkerRowLike_DoWork(object sender, DoWorkEventArgs e){BackgroundWorker bw = sender as BackgroundWorker;SqlConnection _connection = new SqlConnection(“add your connectionsttring here”);SqlDataReader reader;SqlCommand command = (SqlCommand)e.Argument;try{if (_connection.State != ConnectionState.Open){_connection.Open();command.Connection = _connection;reader = command.ExecuteReader();while (reader.Read()){object[ values = new object[reader.FieldCount – 1];reader.GetValues(values); // store the row values into an array of objectbw.ReportProgress(1, values); // Send the row values to the UI threadThread.CurrentThread.Join(10); // Set the thread to sleep 20 ms to let the grid time to update}}}catch (Exception ex){// Add your exception handling here…}finally{if (_connection.State == ConnectionState.Open)_connection.Close();}}In the PreportProgress event we can add the row values to the DataSet.private void _backgroundWorkerRowLike_ProgressChanged(object sender, ProgressChangedEventArgs e){If (e.UserState != null){_ds.Tables[0].LoadDataRow((object[)e.UserState, true);}} When you running the sample each returned row of the DataReader will be send as an array of object to the UI thread and there it can be added to the DataSet. The ultragrid will be add and show each added row automatically. The user of the application will see that the data load is in progress.In the button_Click event you can additionally start showing a “Please wait” control and close/hide ist in the RunWorkerCompleted event. Please note: the code samples are copied out of more complexe code. If it will not be running, please give me a little info. I will create a complete sample app then.Kind regards,Ralf
Hi,
I am using a progress animation to visualize the loading progress. It looks like the progress control the SQL server 2005 is using. When the loading was started, the control will be shown (RotatingProgress.Show() for example). And when the BackgroundWorker raise the Completed event, the control will be hidden.
If you using a DataTable as DataSource for the grid I have some code using the DataReader to load rows just in time into the grid. This code will also using a BackgroundWorker. I have the code not here at work, but I can put it here today.
Kind regards,
Ralf