This repository has been archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnboundColumnHelper.cs
70 lines (62 loc) · 2.8 KB
/
UnboundColumnHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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;
using DevExpress.XtraGrid.Columns;
using DevExpress.Skins;
using DXSample.Properties;
using DevExpress.LookAndFeel;
namespace DXSample {
public class UnboundColumnHelper {
public event EventHandler AsyncOperationIsComplete;
Dictionary<int, object> unboundValues = new Dictionary<int, object>();
private delegate KeyValuePair<int, object> GetDataDelegate(int index);
GridColumn _unboundColumn;
public UnboundColumnHelper(GridColumn unboundColumn) {
_unboundColumn = unboundColumn;
_unboundColumn.View.CustomUnboundColumnData += new DevExpress.XtraGrid.Views.Base.CustomColumnDataEventHandler(View_CustomUnboundColumnData);
}
void View_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e) {
if(e.Column.FieldName == _unboundColumn.FieldName)
if(e.IsGetData) {
object val = GetValue(e);
e.Value = val;
}
}
private object GetValue(DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e) {
if(unboundValues.ContainsKey(e.ListSourceRowIndex)) return unboundValues[e.ListSourceRowIndex];
Skin commonSkin = CommonSkins.GetSkin(UserLookAndFeel.Default.ActiveLookAndFeel);
SkinElement loadingBig = commonSkin["LoadingBig"];
unboundValues.Add(e.ListSourceRowIndex, loadingBig.Image.Image);
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) {
string bitmapPath = string.Format(@"..\..\UserPictures\{0}.png", index % 18);
object val = new Bitmap(bitmapPath);
Thread.Sleep(1500);
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;
RaiseAsyncOperationIsComplete();
}
private void RaiseAsyncOperationIsComplete() {
if(AsyncOperationIsComplete != null)
AsyncOperationIsComplete(this, EventArgs.Empty);
}
}
}