Example E3140
Visible to All Users

WinForms Data Grid - Asynchronously load data into an unbound column

This example handles the CustomUnboundColumnData event to load data in the Quantity unbound column.

C#
private void OnCustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e) { if (e.Column.FieldName == "Quantity") if (e.IsGetData) { object val = GetSummaryValue(e); e.Value = val; } } private object GetSummaryValue(DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e) { if (unboundValues.ContainsKey(e.ListSourceRowIndex)) return unboundValues[e.ListSourceRowIndex]; unboundValues.Add(e.ListSourceRowIndex, "Not Loaded"); GetDataAsync(e.ListSourceRowIndex); return unboundValues[e.ListSourceRowIndex]; }

GetDataAsync asynchronously loads row data:

C#
private void GetDataAsync(int index) { GetDataDelegate d = new GetDataDelegate(GetData); d.BeginInvoke(index, new AsyncCallback(DataLoaded), null); } KeyValuePair<int, object> GetData(int index) { int id = Convert.ToInt32(nwindDataSet.Orders[index]["OrderID"]); OleDbConnection connection = new OleDbConnection(connectionSting); connection.Open(); string cmdText = string.Format("SELECT SUM({0}) FROM {1} WHERE {2} = {3}", "Quantity", "OrderDetails", "OrderID", id); OleDbCommand command = new OleDbCommand(cmdText, connection); object val = command.ExecuteScalar(); Thread.Sleep(500); connection.Close(); return new KeyValuePair<int, object>(index, val); }

Files to Review

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

WindowsApplication3/Main.cs(vb)
C#
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using DevExpress.XtraEditors; using System.Data.OleDb; using System.Threading; using System.Runtime.Remoting.Messaging; namespace DXSample { public partial class Main: XtraForm { string connectionSting = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\nwind.mdb"; Dictionary<int, object> unboundValues = new Dictionary<int, object>(); private delegate KeyValuePair<int, object> GetDataDelegate(int index); public Main() { InitializeComponent(); } private void OnFormLoad(object sender, EventArgs e) { this.ordersTableAdapter.Fill(this.nwindDataSet.Orders); } private void OnCustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e) { if (e.Column.FieldName == "Quantity") if (e.IsGetData) { object val = GetSummaryValue(e); e.Value = val; } } private object GetSummaryValue(DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e) { if (unboundValues.ContainsKey(e.ListSourceRowIndex)) return unboundValues[e.ListSourceRowIndex]; unboundValues.Add(e.ListSourceRowIndex, "Not Loaded"); GetDataAsync(e.ListSourceRowIndex); return unboundValues[e.ListSourceRowIndex]; } private void GetDataAsync(int index) { GetDataDelegate d = new GetDataDelegate(GetData); d.BeginInvoke(index, new AsyncCallback(DataLoaded), null); } KeyValuePair<int, object> GetData(int index) { int id = Convert.ToInt32(nwindDataSet.Orders[index]["OrderID"]); OleDbConnection connection = new OleDbConnection(connectionSting); connection.Open(); string cmdText = string.Format("SELECT SUM({0}) FROM {1} WHERE {2} = {3}", "Quantity", "OrderDetails", "OrderID", id); OleDbCommand command = new OleDbCommand(cmdText, connection); object val = command.ExecuteScalar(); Thread.Sleep(500); connection.Close(); return new KeyValuePair<int, object>(index, val); } void DataLoaded(IAsyncResult r) { GetDataDelegate d = (r as AsyncResult).AsyncDelegate as GetDataDelegate; KeyValuePair<int, object> pair = (KeyValuePair<int, object>)d.EndInvoke(r); unboundValues[pair.Key] = pair.Value; UpdateGrid(); } private void UpdateGrid() { if (gridControl1.InvokeRequired) gridControl1.Invoke(new MethodInvoker(delegate{ gridView1.LayoutChanged(); })); else gridView1.LayoutChanged(); } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.