forked from cyq1162/cyqdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMBindUI.cs
179 lines (177 loc) · 6.48 KB
/
MBindUI.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System;
using System.Web.UI.WebControls;
using System.Text;
using System.Web.UI;
using Win = System.Windows.Forms;
using CYQ.Data.Table;
using CYQ.Data.Xml;
using System.Reflection;
using System.Xml;
namespace CYQ.Data.UI
{
internal class MBindUI
{
public static void Bind(object ct, object source, string nodeID)
{
if (ct == null)
{
return;
}
if (ct is XHtmlAction)
{
#region XHtmlAction 对象处理
XHtmlAction doc = ct as XHtmlAction;
MDataTable dt = source as MDataTable;
doc.LoadData(dt);
XmlNode node = null;
if (string.IsNullOrEmpty(nodeID))
{
doc.SetForeach();
}
else
{
node = doc.Get(nodeID);
if (node != null)
{
doc.SetForeach(node, node.InnerXml);
}
}
#endregion
}
else
{
#region 检测下拉列表控件
if (ct is ListControl)
{
BindList(ct as ListControl, source as MDataTable);
}
else if (ct is Win.ListControl)
{
BindList(ct as Win.ListControl, source as MDataTable);
}
else
{
Type t = ct.GetType();
PropertyInfo p = t.GetProperty("DataSource");
if (p != null)
{
#region DataGridView处理
MethodInfo meth = t.GetMethod("DataBind");
if (meth != null)//web
{
p.SetValue(ct, source, null);
meth.Invoke(ct, null);
}
else
{
if (source is MDataTable)
{
MDataTable dt = source as MDataTable;
source = new MDataView(ref dt);
}
p.SetValue(ct, source, null);//winform
}
#endregion
}
else //wpf,sliverlight
{
p = t.GetProperty("ItemsSource");
if (p != null)
{
MDataTable dt = null;
if (source is MDataTable)
{
dt = source as MDataTable;
source = dt.ToDataTable().DefaultView;
}
p.SetValue(ct, source, null);//winform
p = t.GetProperty("SelectedValuePath");//判断是不是下拉列表
if (p != null)
{
p.SetValue(ct, dt.Columns[0].ColumnName, null);
p = t.GetProperty("DisplayMemberPath");
p.SetValue(ct, dt.Columns[dt.Columns.Count > 1 ? 1 : 0].ColumnName, null);
}
}
}
}
#endregion
}
}
public static void BindList(object ct, MDataTable source)
{
if (ct is ListControl)
{
BindList(ct as ListControl, source);
}
else if (ct is Win.ListControl)
{
BindList(ct as Win.ListControl, source);
}
else //wpf
{
if (source.Columns.Count > 0)
{
Type t = ct.GetType();
PropertyInfo p = t.GetProperty("ItemsSource");
if (p != null)
{
p.SetValue(ct, source, null);
p = t.GetProperty("SelectedValuePath");
if (p != null)
{
p.SetValue(ct, source.Columns[0].ColumnName, null);
p = t.GetProperty("DisplayMemberPath");
p.SetValue(ct, source.Columns[source.Columns.Count > 1 ? 1 : 0].ColumnName, null);
}
}
}
}
}
private static void BindList(Win.ListControl listControl, MDataTable source)
{
try
{
if (source.Columns.Count > 0)
{
listControl.DataSource = new MDataView(ref source);
listControl.ValueMember = source.Columns[0].ColumnName;
listControl.DisplayMember = source.Columns[source.Columns.Count > 1 ? 1 : 0].ColumnName;
}
}
catch (Exception err)
{
Log.WriteLogToTxt(err);
}
}
private static void BindList(ListControl listControl, MDataTable source)
{
if (source.Columns.Count > 0)
{
listControl.DataSource = source;
listControl.DataValueField = source.Columns[0].ColumnName;
listControl.DataTextField = source.Columns[source.Columns.Count > 1 ? 1 : 0].ColumnName;
listControl.DataBind();
}
}
public static string GetID(object ct)
{
Type t = ct.GetType();
if (t.FullName.IndexOf('.') != t.FullName.LastIndexOf('.'))
{
PropertyInfo p = t.GetProperty(t.FullName.Contains(".Web.") ? "ID" : "Name");
if (p == null)
{
return string.Empty;
}
string propName = Convert.ToString(p.GetValue(ct, null));
if (propName.Length > 4 && propName[0] <= 'z')//小母字母开头。
{
propName = propName.Substring(3);
}
return propName;
}
return string.Empty;
}
}
}