Note that there are some explanatory texts on larger screens.

plurals
  1. POPopulating datagridview using asynchronous callback and thread mechanism
    text
    copied!<p>I am new to .NET. I need to complete the following assignment using asynchronous callback mechanism in C# winforms.</p> <p>On a form load I need to get the data from database and populate in a datagridview. The retrieval may take a long time and I want to use the UI meanwhile. Also I need a callback mechanism to check whether datagridview is populated or not. I have to use thread and async callback mechanism to achieve this. </p> <pre><code> private CustomerEntities cn = null; delegate CustomerEntities DataSourceDelegate(); delegate void loadGridViewDelegate(CustomerEntities dtCustomers); DataSourceDelegate delegate_GetCustomers; public CustomerEntities DataSource() { cn = new CustomerEntities(); return cn; } private void Form1_Load(object sender, EventArgs e) { status.Text = "Loading"; delegate_GetCustomers = new DataSourceDelegate(DataSource); delegate_GetCustomers.BeginInvoke(LoadCustomerCallBack, null); } private void LoadCustomerCallBack(IAsyncResult ar) { CustomerEntities dtCutomers; dtCutomers = delegate_GetCustomers.EndInvoke(ar); loadGridView(dtCutomers); } private void loadGridView(CustomerEntities dtCutomers) { if (dataGridView.InvokeRequired) { loadGridViewDelegate del = new loadGridViewDelegate(loadGridView); dataGridView.Invoke(del, new CustomerEntities[] { dtCutomers }); } else { dataGridView.DataSource = dtCutomers.customerDetails; } } </code></pre> <p>The datagridview is getting populated correctly but the UI is blocked when I'm trying to access it when the function is retrieving the data. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload